visual-studio 在 Visual Studio 中调试时如何设置特定的环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/100543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set specific environment variables when debugging in Visual Studio?
提问by Tobi
On a class library project, I set the "Start Action" on the Debug tab of the project properties to "Start external program" (NUnitin 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?)
在类库项目中,我将项目属性的“调试”选项卡上的“启动操作”设置为“启动外部程序”(在本例中为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 应用程序的环境变量(COMplus_Version,它设置运行时版本),因此在系统范围内设置它确实不是一个选项。
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...
作为一种解决方法,我只是通过将其设置为 .NET 强制 NUnit 在正确的 .NET 版本 (2.0) 中启动nunit.exe.config,但不幸的是,这也意味着我所有的 .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...))
(我保持这个问题的开放性(不接受答案),以防有人碰巧知道如何(毕竟它可能对其他目的也有用......))
回答by John Dibling
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.
打开您的项目。转到 Project -> Properties... 在 Configuration Properties -> Debugging 下,编辑“Environment”值以设置环境变量。
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”添加到路径中,请将“环境”值设置为“PATH=%PATH%;c:\foo\bin”。
回答by CRUZ
In Visual Studio for Mac and C# you can use:
在 Visual Studio for Mac 和 C# 中,您可以使用:
Environment.SetEnvironmentVariable("<Variable_name>", "<Value>");
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);
回答by CRUZ
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 / #endifto make sure only certain builds have it.
我做在C / C ++是用_putenv()在main()和设置任何变量。通常我用 a 包围它以#if defined DEBUG_MODE / #endif确保只有某些版本有它。
_putenv("MYANSWER=42");
I believe you can do the same thing with C# using os.putenv(), i.e.
我相信你可以使用 os.putenv() 对 C# 做同样的事情,即
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 进程设置环境变量,因此是一个临时设置,这正是您要寻找的。
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.
顺便说一下,使用进程资源管理器 ( http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx)很好,它是一个 sysinternals 工具。您可以看到给定进程的环境变量副本是什么,因此您可以验证您设置的内容是您获得的内容。
回答by tymtam
Starting with NUnit 2.5 you can use /framework switch e.g.:
从 NUnit 2.5 开始,您可以使用 /framework 开关,例如:
nunit-console myassembly.dll /framework:net-1.1
This is from NUnit's help pages.
这是来自NUnit 的帮助页面。
回答by Tejas Katakdhond
回答by OJ.
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。
回答by Mark
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 文件来设置您的环境,那么您唯一可能的选择是设置一个系统范围的环境变量。你可以通过这样做找到这些
- Right click "My Computer"
- Select properties
- Select the "advanced" tab
- Click the "environment variables" button
- In the "System variables" section, add the new environment variable that you desire
- "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
我不知道您是否必须重新启动 Visual Studio,但似乎不太可能。HTH
回答by Jeff Yates
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 编写一个加载项,在您执行启动之前修改其环境变量。我不确定将其放入您的流程中有多容易。


