Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Environment.GetEnvironmentVariable(...) Not Working on OSX

I am running a console application on OSX that has the following...

public static void main(string[] args)
{
    string mongoEndpoint = Environment.GetEnvironmentVariable("MONGO_ENDPOINT");

    if(string.IsNullOrEmpty(mongoEndpoint))
    {
        _log.Fatal("Invalid Mongo Endpoint");
        throw new Exception("Invalid Mongo Endpoint");
    }
}

I have added a MONGO_ENDPOINT environment variable to my ~/.bash_profile and when I run echo $MONGO_ENDPOINT it outputs the correct value.

However, when I run my console application from Xamarin Studio in OSX, it returns null.

I tried running the following to see if it was all environment variables, and it returned the correct value.

Console.Out.WriteLine(Environment.GetEnvironmentVariable("HOME"));

and it outputted /usr/myname perfectly fine.

Any reason why it would not be able to find this environment variable?

like image 799
TheJediCowboy Avatar asked Feb 26 '16 04:02

TheJediCowboy


1 Answers

OS-X GUI apps will not inherit your private/custom env vars that are defined via a shell (bash, zsh, etc...) if they are launched from Finder/Spotlight. You have two choices:

1) Start Xamarin Studio from the cmd line so your current users' environment will be used:

/Applications/Xamarin\ Studio.app/Contents/MacOS/XamarinStudio &

2) If you want user environment vars available to GUI apps launched from Finder/Spotlight you need to modify your /etc/launchd.conf, see this SO answer:

https://stackoverflow.com/a/3756686/4984832

Runtime Configuration Guidelines

like image 113
SushiHangover Avatar answered Oct 02 '22 20:10

SushiHangover