16

I have a C# project in Visual Studio 2010 and I wish to run/debug my application with a specific environment variable in place.

This strikes me as a feature that probably exists somewhere, but I can't find it despite some extensive searching. This question relates to 2008 and below and doesn't contain an answer that helps me. This question relates to the build process, not the act of debugging/running.

I appreciate a work-around would be to start my IDE with the environment variables in place, but I'd rather control this from the IDE. Is this possible?

Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • If I understand: do you want to manage an environment variable into the IDE and pass to your application while running? – michele Jun 25 '13 at 12:38
  • @michele I want to start a debugging session and specify an environment variable that will be made available to my application. In Eclipse, this would be trivially achieved using a "Run configuration". – Duncan Jones Jun 25 '13 at 12:48
  • Have you tried to use config files? – michele Jun 25 '13 at 13:02

3 Answers3

12

It's not as clean as setting it from outside the application being debugged, but you can add to the Main something like this (NB I'm a VB programmer):

#if (DEBUG)
    Environment.SetEnvironmentVariable("YourVar", "YourVal");
#endif
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
4

This is possible in the C++ IDE, not the C# IDE. I'd guess it was omitted intentionally because C# has better ways to configure a program. Environment variables are awkward since they require an installer that tinkers with the user's system environment when the app is deployed. That's brittle, another installer can easily destroy that and they often do.

The C# way is to use an application setting. Project + Properties, Settings tab.

A possible alternative is to use a command line argument. You'll get it in your Main() method, you specify a value in the Project + Properties, Debug tab.

You can still get what you want with a trick that takes using the C++ IDE to start your program:

  • Add a new project to your solution and select the Visual C++, General, Makefile project template.
  • Click Finish right away, the wizard asks too many questions.
  • Right-click the added project, Properties, select the NMake node.
  • Edit the "Build Command Line" setting, and set it to "echo Done".
  • Edit the "Output" setting, set it to the full path of your C# executable.
  • Select the Debugging node, change the Debugger type to Managed Only.
  • And you'll see the one below that, what you want, edit the "Environment" setting.
  • Right-click the project again, pick "Set as Startup Project".
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 5
    Using environment variables does not necessarily require installers to modify the user's system environment. Environment variables are scoped per-process, normally inheriting from the system, but not always. That is why `ProcessStartInfo.EnvironmentVariables` exists. In a multi-process application, they are an alternative means of providing startup configuration to a program when the command line args API cannot be modified or utilized. IMO omitting this setting for the C# debugger is a mistake. – nicholas Mar 01 '16 at 14:26
  • 1
    Another circumstance in which this is useful is when you are developing an application that will be hosted by a third party which communicates information to your program via environment variables. Environment variables are alive and well, and they have many uses. Omitting them was a mistake for sure. – John Hargrove Jul 24 '17 at 18:58
2

For C# debugging with environment variables under Visual Studio 2013, what I do is open up the "Developer Command Prompt for VS2013" in the start menu under Visual Studio. From the command prompt I set the environment vars that I want and then run "devenv.exe" to launch Studio. Next open a solution and start debugging.

Keep in mind that if you want to change your environment vars you will need to stop debugging, quit visual studio and then tweak the vars in that open command prompt, then start again. Remember that an environment moves forward as a process (CMD.EXE) launches the next (DEVENV.EXE) and then the next (YourApp). Changes at the very beginning aren't moved forward, you need to start the chain over.

John Dyer
  • 2,316
  • 1
  • 21
  • 27