OSX Pro Tip for .NET Environment Variables

Marcos Placona
Nov 22, 2016 · 2 min read
Image for post
Image for post

I use environment variables in all my apps to make sure none of my secret keys end up in GitHub. In C#, if you want to get environment variables in your app you just need to use the Environment class and call the method `GetEnvironmentVariable()` passing the name of the variable you’ve already defined.

accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");

I have recently switched from building and running apps directly on the terminal and Visual Studio Code to using Visual Studio for Mac. I opened up an application I had already built and knew worked and tried it out. I started to get some weird errors where it would claim to not find my environment variables.

I went ahead and run my application from terminal and it worked as expected.

A bit of Googling and I landed on this StackOverflow thread. But here’s the gist of it:

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

The solution to that is to instead of starting Visual Studio for Mac from finder, you can start it from terminal and it will then inherit your environment variables. You can start it like this:

/Applications/Visual\ Studio.app/Contents/MacOS/VisualStudio &

But remembering that will be hard, and you will end up tabbing through your terminal until you get to it every single time. So here’s an alias:

alias vs='/Applications/Visual\ Studio.app/Contents/MacOS/VisualStudio &'

If you run that on your terminal, you can then just run `vs` afterwards, and Visual Studio for Mac will start inheriting your environment variables.

This will disappear every time you restart your terminal though, so add it to your `.bash_profile` or equivalent if you wanna make it permanent.

Originally published at www.placona.co.uk.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store