73

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 applications (COMplus_Version, it sets the 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 its own configuration file...

(I am keeping the question open (not accepting an answer) in case someone does happen to find out how (it might be useful for other purposes too after all...))

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Tobi
  • 71,900
  • 5
  • 30
  • 36

9 Answers9

90

In Visual Studio 2008 and Visual Studio 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 application, set the 'Environment' value to "PATH=%PATH%;c:\foo\bin".

Here's a screenshot of the settings dialog

Colonel Panic
  • 126,394
  • 80
  • 383
  • 450
John Dibling
  • 97,027
  • 28
  • 181
  • 313
  • 1
    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 Nov 19 '08 at 15:41
  • I will edit this post with a screenshot of where the settings are. – John Dibling Nov 21 '08 at 15:21
  • 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 Dec 12 '08 at 10:54
  • 11
    This is properties for C/C++ projects. I guess you are using C# or VB.NET – Juozas Kontvainis Mar 03 '09 at 14:04
  • 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 Mar 03 '09 at 15:58
  • sadly, this is not available for remote debugging. – Bahbar Nov 27 '09 at 10:13
  • Also doesn't seem to be available for Web projects. – Ryan Jun 14 '10 at 20:18
  • There is a bug which forbids to add several variables. See workaround in : https://connect.microsoft.com/VisualStudio/feedback/details/739477/visual-c-2010-set-multiple-variables-for-project-debug-environment – Alex Mar 06 '14 at 16:20
  • The screenshot seems not to be available (is blank) - broken link? – Peter Mortensen May 23 '17 at 08:42
  • @PeterMortensen: Fixed. – John Dibling May 23 '17 at 18:03
  • 12
    Nothing remotely similar to this in VS2017 – dudeNumber4 Apr 23 '18 at 20:13
11

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 full list of variables with this:

foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
            Console.WriteLine("  {0} = {1}", de.Key, de.Value);
CRUZ
  • 127
  • 1
  • 3
  • OMG, I've been searching for the last half hour to find that simple call Environment.SetEnvironmentVariable(). Thanks! – Alan Dec 05 '17 at 18:16
  • This explains how to set an environment variable globally from code. The question explicitly mentions this as undesireable. – Oliver Salzburg May 25 '20 at 08:59
8

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.

Mariusz Jamro
  • 29,116
  • 24
  • 107
  • 151
4

In Visual Studio 2019 right-click your project, choose Properties. In the project properties window, select the Debug tab. Then, under Environment variables change the value of your environment from Development to Production or other environments. For .Net Core and .Net 5 the property is called ASPNETCORE_ENVIRONMENT.

enter image description here

Wouter Vanherck
  • 1,670
  • 3
  • 23
  • 38
  • Pretty sure this _only_ works for `.Net Core|5+` because my Debug tab doesn't have that `Environment variables` stuff at all. Or perhaps it's a Console App vs. WebSite|WebAPI thing? – Scott Fraley Oct 13 '21 at 22:55
  • @ScottFraley The example was through an ASP .NET Core 5 project – Wouter Vanherck Oct 14 '21 at 08:17
1

Starting with NUnit 2.5 you can use /framework switch e.g.:

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

This is from NUnit's help pages.

tymtam
  • 24,269
  • 5
  • 71
  • 101
0

Set up a batch file which you can invoke. Pass the path the batch file, and have the batch file set the environment variable and then invoke NUnit.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
OJ.
  • 28,259
  • 5
  • 53
  • 70
  • 5
    Visual Studio refuses to accept anything but executables (.exe) in the path for the program to start. – Tobi Sep 19 '08 at 08:57
0

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
  • 9,748
  • 2
  • 36
  • 41
  • Mark, I believe the requirement was for the environment the program was started in, not the user or system environments. – OJ. Sep 20 '08 at 08:29
  • 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 Sep 20 '08 at 09:50
  • 1
    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 Sep 09 '09 at 06:54
0

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. I am not sure how easy that would be to put into your process.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jeff Yates
  • 60,114
  • 20
  • 136
  • 187
0

You can set it at Property > Configuration Properties > Debugging > Environment enter image description here