什么是环境variables概念?
在一个C#程序中,我需要调用一个可执行文件。 可执行文件将调用驻留在同一文件夹中的其他可执行文件。 可执行文件依赖于两个环境variables“PATH”和“RAYPATH”来正确设置。 我尝试了以下两件事情:
当我运行进程时,系统找不到可执行文件(“executeable1”)。 我试图将StartInfo.FileName设置为“executeable1”的完整path – 但是然后在“executeable1”中调用form的EXE文件没有find…
我如何处理这个?
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();
你的问题实际上是什么? 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);
有许多类型的环境变量,如系统级别和用户。 这取决于你的要求。
要设置环境变量,只需使用Get Set方法即可。 传递变量名称和值作为参数,如果用来定义访问级别,则必须通过它。 要访问该值,请使用Set方法来传递访问级别参数。
例如,我正在定义用户级别的变量:
//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);