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?
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
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With