12

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.

Community
  • 1
  • 1
Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
  • 1
    Strange how that answer got so many votes. He actually described a setting that is only available for C++ projects, as noted in a comment. You'll of course have to tell us what you want to accomplish to get an answer that is actually useful. – Hans Passant May 20 '17 at 12:25
  • @HansPassant 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. – Philip Tenn May 20 '17 at 12:28
  • 1
    Don't tell me, put it in your question. Also explain why you can't simply use Environment.SetEnvironmentVariable() or a config setting or Debugger.IsAttached to accomplish this. – Hans Passant May 20 '17 at 12:48
  • 3
    @HansPassant Which one of those approaches entails *not* modifying my source code everytime I want to switch between Dev and Production? I want to be able to build/run Debug and have it configured for Debug, and then build Release and have it configured for Production. Also, please keep in mind I'm coming from a Java background, so I am not completely familiar with these different approaches. – Philip Tenn May 20 '17 at 15:11

3 Answers3

2

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.

cskwg
  • 790
  • 6
  • 13
2

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.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
0

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.

nelion
  • 1,712
  • 4
  • 17
  • 37