在Visual Studio中调试时如何设置特定的环境变量?

时间:2021-07-08 11:48:41

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?)

在类库项目中,我在项目属性的Debug选项卡上设置了“Start Action”,以“启动外部程序”(在本例中为NUnit)。我想在这个程序启动的环境中设置一个环境变量。我该怎么做呢?(它甚至可能吗?)

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.

它是一个影响所有。net应用程序的环境变量(它设置了运行时版本),因此设置它的系统范围真的不是一个选项。

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...

作为一种解决方案,我只是强迫NUnit以正确的。net版本(2.0)开始,并将其设置在NUnit .exe中。但不幸的是,这也意味着我所有的。net 1.1单元测试现在也在。net 2.0中运行。我应该复制一个可执行文件,这样它就可以有自己的配置文件…

(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...))

(我一直在保持这个问题的开放性(不接受一个答案),以防万一有人碰巧发现了这个问题(它可能在其他方面也是有用的)。

7 个解决方案

#1


60  

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

在Visual Studio 2008和Visual Studio 2005中,至少可以在项目设置中指定对环境变量的更改。

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".

例如,如果您想在调试应用程序时将目录“c:\foo\bin”添加到路径中,请将“Environment”值设置为“path =% path %;c:\foo\bin”。

在Visual Studio中调试时如何设置特定的环境变量?

#2


4  

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

Visual Studio 2003似乎不允许您为调试设置环境变量。

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.

我在C/ c++中所做的是在main()中使用_putenv()并设置任何变量。通常我用一个#if定义的DEBUG_MODE / #endif来包围它,以确保只有特定的构建具有它。

_putenv("MYANSWER=42");

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

我相信您可以使用os.putenv()来做同样的事情。

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.

这些将为该shell进程设置envrironment变量,并且由于这是一个临时设置,这就是您要寻找的。

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.

顺便说一下,使用process explorer (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx)是一个很好的方法,它是一个sysinternals工具。您可以看到给定进程的环境变量的副本是什么,因此您可以验证所设置的是您所得到的。

#3


2  

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

在Visual Studio中,Mac和c#可以使用:

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

环境。SetEnvironmentVariable(“< Variable_name >”、“ <价值> ”);

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);

#4


1  

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

如果您不能使用bat文件来设置您的环境,那么您唯一可能的选择是设置一个系统范围的环境变量。你可以通过做这些来找到它们。

  1. Right click "My Computer"
  2. 右击“我的电脑”
  3. Select properties
  4. 选择属性
  5. Select the "advanced" tab
  6. 选择“高级”选项卡
  7. Click the "environment variables" button
  8. 点击“环境变量”按钮。
  9. In the "System variables" section, add the new environment variable that you desire
  10. 在“系统变量”部分中,添加您想要的新环境变量。
  11. "Ok" all the way out to accept your changes
  12. “好”一直到接受你的改变。

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

我不知道你是否需要重启visual studio,但似乎不太可能。HTH

#5


1  

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

从NUnit 2.5开始,你可以使用/框架切换。

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

This is from NUnit's help pages.

这是来自NUnit的帮助页面。

#6


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.

设置可以调用的批处理文件。通过该批处理文件的路径,并让批处理文件设置环境变量,然后调用NUnit。

#7


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.

当环境从父进程继承时,您可以为Visual Studio编写一个加载项,在执行开始之前修改它的环境变量。我不知道在你的过程中有多容易。

#1


60  

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

在Visual Studio 2008和Visual Studio 2005中,至少可以在项目设置中指定对环境变量的更改。

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".

例如,如果您想在调试应用程序时将目录“c:\foo\bin”添加到路径中,请将“Environment”值设置为“path =% path %;c:\foo\bin”。

在Visual Studio中调试时如何设置特定的环境变量?

#2


4  

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

Visual Studio 2003似乎不允许您为调试设置环境变量。

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.

我在C/ c++中所做的是在main()中使用_putenv()并设置任何变量。通常我用一个#if定义的DEBUG_MODE / #endif来包围它,以确保只有特定的构建具有它。

_putenv("MYANSWER=42");

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

我相信您可以使用os.putenv()来做同样的事情。

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.

这些将为该shell进程设置envrironment变量,并且由于这是一个临时设置,这就是您要寻找的。

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.

顺便说一下,使用process explorer (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx)是一个很好的方法,它是一个sysinternals工具。您可以看到给定进程的环境变量的副本是什么,因此您可以验证所设置的是您所得到的。

#3


2  

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

在Visual Studio中,Mac和c#可以使用:

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

环境。SetEnvironmentVariable(“< Variable_name >”、“ <价值> ”);

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);

#4


1  

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

如果您不能使用bat文件来设置您的环境,那么您唯一可能的选择是设置一个系统范围的环境变量。你可以通过做这些来找到它们。

  1. Right click "My Computer"
  2. 右击“我的电脑”
  3. Select properties
  4. 选择属性
  5. Select the "advanced" tab
  6. 选择“高级”选项卡
  7. Click the "environment variables" button
  8. 点击“环境变量”按钮。
  9. In the "System variables" section, add the new environment variable that you desire
  10. 在“系统变量”部分中,添加您想要的新环境变量。
  11. "Ok" all the way out to accept your changes
  12. “好”一直到接受你的改变。

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

我不知道你是否需要重启visual studio,但似乎不太可能。HTH

#5


1  

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

从NUnit 2.5开始,你可以使用/框架切换。

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

This is from NUnit's help pages.

这是来自NUnit的帮助页面。

#6


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.

设置可以调用的批处理文件。通过该批处理文件的路径,并让批处理文件设置环境变量,然后调用NUnit。

#7


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.

当环境从父进程继承时,您可以为Visual Studio编写一个加载项,在执行开始之前修改它的环境变量。我不知道在你的过程中有多容易。