views:

14124

answers:

5

On a class library project, I set the "Start Action" on the Debug tab of the project properties to "Start external program" (NUnit in this case). I want to set an environment variable in the environment this program is started in. How do I do that? (Is it even possible?)

EDIT:

It's an environment variable that influences all .NET apps (COMplus_Version, it sets runtime version) so setting it system wide really isn't an option.

As a workaround I just forced NUnit to start in right .NET version (2.0) by setting it in nunit.exe.config, though unfortunately this also means all my .NET 1.1 unit tests are now also run in .NET 2.0. I should probably just make a copy of the executable so it can have it's own config file...

Keeping the question open in case someone does happen to find out how (might be useful for other purposes too after all...)

A: 

Why not set up a batch file which you can invoke? Pass the path the batch file, have the batch file set the environment variable and then invoke NUnit?

OJ
Visual Studio refuses to accept anything but executables (.exe) in the path for the program to start.
Tobi
+1  A: 

If you can't use bat files to set up your environment, then your only likely option is to set up a system wide environment variable. You can find these by doing

  1. Right click "My Computer"
  2. Select properties
  3. Select the "advanced" tab
  4. Click the "environment variables" button
  5. In the "System variables" section, add the new environment variable that you desire
  6. "Ok" all the way out to accept your changes

I don't know if you'd have to restart visual studio, but seems unlikely. HTH

Mark
Mark, I believe the requirement was for the environment the program was started in, not the user or system environments.
OJ
Yeah, it's an environment variable that influences all .NET apps (COMplus_Version, it sets runtime version) so setting it system wide really isn't an option.
Tobi
Yup - missed that. Sorry.
Mark
I got thru by defining a user-level Environment variable (My Computer > Properties > Advanced). Launch a new instance of the command shell and echo %NEW_VAR% just to be sure. Start a new instance of devenv and debug away.
Gishu
+9  A: 

In VS 2008 and VS 2005 at least, you can specify changes to environment variables in the project settings.

Open your project. Go to Project>Properties... Under Configuration Properties>Debugging, edit the 'Environment' value to set environment variables.

For example, if you want to add the directory "c:\foo\bin" to the path when debugging your app, set the 'Environment' value to "PATH=%PATH%;c:\foo\bin".

Here's a screenshot of the setting s dialog:

John Dibling
I'm using VS2008, and my project properties doesn't have "Configuration Properties" or "Debugging" on it. I can't find anything about environment variables anywhere under the project properties. What am I doing wrong?
Stewart Johnson
I will edit this post with a screenshot of where the settings are.
John Dibling
Hi, how did you get to this screen? In my VS2008 and VS2005 project properties seem to look differently, and they don't have environment settings
Grzenio
This is properties for C/C++ projects. I guess you are using C# or VB.NET
Juozas Kontvainis
Hi, all. I noticed that several of my developers, who all work in C++, also could not see these properties. In the end it turned out to be how MSVC was installed. When they reinstalled MSVC with every option enabled, they were able to see all these properties, along with other IDE functionality.
John Dibling
sadly, this is not available for remote debugging.
Bahbar
Also doesn't seem to be available for Web projects.
Ryan
A: 

As environments are inherited from the parent process, you could write an add-in for visual studio that modifies its environment variables before you perform the start. Not sure how easy that would be to put into your process.

Jeff Yates
A: 

Visual Studio 2003 doesn't seem to allow you to set environment variables for debugging.

What I do in C/C++ is use _putenv() in main() and set any variables. Usually I surround it with a #if defined DEBUG_MODE / #endif to make sure only certain builds have it.

_putenv("MYANSWER=42");

I believe you can do the same thing with C# using os.putenv(), i.e.

os.putenv('MYANSWER', '42');

These will set the envrironment variable for that shell process only, and as such is an ephemeral setting, which is what you are looking for.

By the way, its good to use process explorer (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx), which is a sysinternals tool. You can see what a given process' copy of the environment variables is, so you can validate that what you set is what you got.