How to set certain environment variables when debugging in Visual Studio?

In the class library project, I set the "Run Action" option on the "Debug" tab of the "Run External Program" project properties ( NUnit in this case). I want to set an environment variable in the environment in which this program is running. How should I do it? (Is it possible?)

EDIT:

This is an environment variable that affects all .NET applications (COMplus_Version, it sets the execution version), so installing it on a system scale is really not an option.

As a workaround, I just got NUnit to start with the right version of .NET (2.0) by installing it in nunit.exe.config , although unfortunately this also means that all my .NET unit tests now also run in .NET 2.0. I should probably just make a copy of the executable so that it can have its own configuration file ...

(I leave the question open (I don’t accept the answer) if someone accidentally finds out how this can be useful for other purposes, too ...))

+39
debugging visual-studio environment-variables
Sep 19 '08 at 8:45
source share
7 answers

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

Open the project. Go to Project β†’ Properties ... In the "Configuration Properties" β†’ "Debugging" section, edit the "Environment" value to set the environment variables.

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

Here is a screenshot of the settings dialog

+60
Sep 30 '08 at 22:24
source share

Visual Studio 2003 does not seem to allow you to set environment variables for debugging.

In C / C ++, I use _putenv() in main() and set any variables. I usually surround it with #if defined DEBUG_MODE / #endif to make sure it only has specific assemblies.

 _putenv("MYANSWER=42"); 

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

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

They will set the envrironment variable only for this shell process, and thus this is the ephemeral setting you are looking for.

By the way, it is useful to use a process handler ( http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx ), which is a sysinternals tool. You can see what an instance of the environment variables of this process is, so you can confirm that what you set is what you got.

+4
Jul 16 '09 at 20:52
source share

In Visual Studio for Mac and C #, you can use:

Environment.SetEnvironmentVariable("<Variable_name>", "<Value>");

But you will need the following namespace

 using System.Collections; 

you can check the complete list of variables as follows:

 foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) Console.WriteLine(" {0} = {1}", de.Key, de.Value); 
+2
Feb 18 '17 at 22:16
source share

If you cannot use bat files to configure your environment, then your only likely option is to set up a system environment variable. You can find them by doing

  • Right click on My Computer
  • Select properties
  • Select the Advanced tab
  • Click the "environment variables" button
  • In the System Variables section, add the new environment variable you want
  • Good to accept your changes.

I don’t know if you have to restart the visual studio, but it seems unlikely. NTN

+1
Sep 19 '08 at 13:16
source share

Starting with NUnit 2.5, you can use the / framework switch, for example:

 nunit-console myassembly.dll /framework:net-1.1 

This is from the NUnit help pages .

+1
Feb 09 2018-12-12T00:
source share

Set up a batch file that you can call. Pass the path to the batch file and the batch file sets the environment variable, and then call NUnit.

0
Sep 19 '08 at 8:53
source share

Because environments inherit from the parent process, you can write an add-in for Visual Studio that modifies its environment variables before starting. I'm not sure how easy it will be in your process.

0
Oct 02 '08 at 4:49
source share



All Articles