Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 830
Philip Tenn Avatar asked May 20 '17 12:05

Philip Tenn


People also ask

How do I set debug configuration in Visual Studio?

In Solution Explorer, right-click the project and choose Properties. In the side pane, choose Build (or Compile in Visual Basic). In the Configuration list at the top, choose Debug or Release. Select the Advanced button (or the Advanced Compile Options button in Visual Basic).

How do I enable debugging in Visual Studio?

In the Visual Studio toolbar, make sure the configuration is set to Debug. To start debugging, select the profile name in the toolbar, such as <project profile name>, IIS Express, or <IIS profile name> in the toolbar, select Start Debugging from the Debug menu, or press F5.

How do I set an environment variable permanently?

You can set an environment variable permanently by placing an export command in your Bash shell's startup script " ~/. bashrc " (or "~/. bash_profile ", or " ~/. profile ") of your home directory; or " /etc/profile " for system-wide operations.

Is it necessary to set environment variable?

Java does not need any environment variables to be set. However, setting some environment variables makes some things easier. PATH If the jre/bin folder is on the path, you don't have to qualify to run the java command. If the jdk/bin folder is on the path, you don't have to qualify to run the java and javac commands.


2 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.

like image 139
cskwg Avatar answered Sep 20 '22 09:09

cskwg


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.

like image 39
Ian Kemp Avatar answered Sep 20 '22 09:09

Ian Kemp