Need to set Debug-specific Environment Variable in VS.NET 2017

Viewed 6162

All,

I apologize if this is the world's dumbest question.

I found this: https://stackoverflow.com/a/155363/463196

I can't find the menu item when following the answer:enter image description here

I guess I need a visual walkthrough for dummies, unless this has changed from VS.NET 2008 and VS.NET 2017.

Edit:

The reason I need this ... I am doing C#, Azure. There is an environment variable that if set for Debug, will make it so the Storage goes to a mock instead of a live Azure Storage. I need this so that I can happily go from test to Prod w/out having touches on my App.Config file.

3 Answers

You are mixing something up here! There are Debug specific variables for the compiler and these are set within the Visual Studio IDE. But this is not what you need! You'll want to access real environment variables from the Windows operating system. See How do I get and set Environment variables in C#? for details. And, in case you don't know, these can be set from a CMD prompt by using the 'SET' command or from the 'Advanced system settings' of the Windows Control Panel. If your application is running within IIS, you may need to restart the machine to get the latest environment variables within your program. Every program always inherits the environment variables from its parent process.

To answer the actual question that you asked, modify your project file to use the MSBuild AfterBuild target with the Exec task. For example:

<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Debug' ">
    <Exec Command="set MY_ENVIRONMENT_VARIABLE_NAME=\"DEBUG\"" />
</Target>
<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release' ">
    <Exec Command="set MY_ENVIRONMENT_VARIABLE_NAME=\"RELEASE\"" />
</Target>

Since you mentioned app.config, a more elegant solution might be to look into automatically transforming that file on build (which entails substituting appropriate values for the specific release configuration), again via MSBuild. [An example of this, using the TransformXml MSBuild task, can be found on this very site.]

For a less manual process, you can install Microsoft's own SlowCheetah extension which will give you an option to add and configure these transforms directly within Visual Studio. This extension is basically just a UI wrapper around TransformXml, and as such does not need to be present on any CI/build machines.

This might be an old post, but the answers so far given doesn't do what I think the OP is asking for. I am working with Azure right now and what you are asking for is what I've done in my project.

So basically I have my environment variables set in Azure and when debugging, I can't access them, but there is a solution for that.

Just go to Project Properties => Debug and from there add the specific environment variables.

enter image description here

This way, when testing locally, you will get the same output in regards to environment variables as when actually connecting to Azure.

Related