C# 为进程设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14553830/
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
Set environment variables for a process
提问by timkado
What is the environment variable concept?
什么是环境变量概念?
In a C# program I need to call an executable. The executable will call some other executables that reside in the same folder. The executables rely on the two environment variables "PATH" and "RAYPATH" to be set correctly. I tried the following two things:
在 C# 程序中,我需要调用一个可执行文件。可执行文件将调用驻留在同一文件夹中的其他一些可执行文件。可执行文件依赖于正确设置两个环境变量“PATH”和“RAYPATH”。我尝试了以下两件事:
- I created a process and set the two varables in StartInfo. The variables exist already but are missing the needed information.
- I tried to set the variables with System.Environment.SetEnvironmentVariable().
- 我创建了一个进程并在 StartInfo 中设置了两个变量。变量已经存在,但缺少所需的信息。
- 我尝试使用 System.Environment.SetEnvironmentVariable() 设置变量。
When I run the process the system can't find the executable ("executeable1"). I tried to set StartInfo.FileName to the full path of "executeable1" - however then the EXE files called form within "executeable1" are not found...
当我运行该进程时,系统找不到可执行文件(“executeable1”)。我试图将 StartInfo.FileName 设置为“executeable1”的完整路径 - 但是然后找不到“executeable1”中名为 form 的 EXE 文件...
How do I deal with this?
我该如何处理?
string pathvar = System.Environment.GetEnvironmentVariable("PATH");
System.Environment.SetEnvironmentVariable("PATH", pathvar + @";C:\UD_\bin\DAYSIM\bin_windows\;C:\UD_\bin\Radiance\bin\;C:\UD_\bin\DAYSIM;");
System.Environment.SetEnvironmentVariable("RAYPATH", @"C:\UD_\bin\DAYSIM\lib\;C:\UD_\bin\Radiance\lib\");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WorkingDirectory = @"C:\UD_\bin\DAYSIM\bin_windows";
//string pathvar = p.StartInfo.EnvironmentVariables["PATH"];
//p.StartInfo.EnvironmentVariables["PATH"] = pathvar + @";C:\UD_\bin\DAYSIM\bin_windows\;C:\UD_\bin\Radiance\bin\;C:\UD_\bin\DAYSIM;";
//p.StartInfo.EnvironmentVariables["RAYPATH"] = @"C:\UD_\bin\DAYSIM\lib\;C:\UD_\bin\Radiance\lib\";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "executeable1";
p.StartInfo.Arguments = arg1 + " " + arg2;
p.Start();
p.WaitForExit();
回答by ken2k
What is your problem actually? System.Environment.SetEnvironmentVariable
changes the environment variables of the current process. If you want to change the variables of a process you create, just use the EnvironmentVariables
dictionary property:
你的问题是什么?System.Environment.SetEnvironmentVariable
改变当前进程的环境变量。如果要更改创建的进程的变量,只需使用EnvironmentVariables
字典属性:
var startInfo = new ProcessStartInfo();
// Sets RAYPATH variable to "test"
// The new process will have RAYPATH variable created with "test" value
// All environment variables of the created process are inherited from the
// current process
startInfo.EnvironmentVariables["RAYPATH"] = "test";
// Required for EnvironmentVariables to be set
startInfo.UseShellExecute = false;
// Sets some executable name
// The executable will be search in directories that are specified
// in the PATH variable of the current process
startInfo.FileName = "cmd.exe";
// Starts process
Process.Start(startInfo);
回答by Dikchani
There are many types of environment variables, like system level and users. It depends on your requirements.
有许多类型的环境变量,如系统级别和用户。这取决于您的要求。
For setting environment variables, just use the Get Set method. Pass variables Name and Value as parameters and if use to define access level then must pass with it. For accessing the value then use the Set method to pass the access level parameter too.
对于设置环境变量,只需使用 Get Set 方法。将变量 Name 和 Value 作为参数传递,如果用于定义访问级别,则必须传递它。要访问该值,请使用 Set 方法也传递访问级别参数。
For example, I am defining user-level variables:
例如,我正在定义用户级变量:
//For setting and defining variables
System.Environment.SetEnvironmentVariable("PathDB", txtPathSave.Text, EnvironmentVariableTarget.User);
System.Environment.SetEnvironmentVariable("DBname", comboBoxDataBaseName.Text, EnvironmentVariableTarget.User);
//For getting
string Pathsave = System.Environment.GetEnvironmentVariable("PathDB", EnvironmentVariableTarget.User);
string DBselect = System.Environment.GetEnvironmentVariable("DBname", EnvironmentVariableTarget.User);