设置环境变量的过程 [英] Set Environment Variable for process

查看:410
本文介绍了设置环境变量的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我了解环境变量的概念?
在C#程序中我需要调用一个可执行文件。该可执行文件将调用位于同一文件夹中的一些其他可执行文件。该可执行文件依赖于两个环境变量PATH和射线路径,以设置正确。我尝试了以下两件事情:


  1. 我创建了一个进程,并设置StartInfo的两个varables。该
    已经存在变量,但缺少必要的信息。

  2. 我试图设置的变量
        System.Environment.SetEnvironmentVariable()。

当我运行过程中系统无法找到可执行文件(executeable1)。我试图StartInfo.FileName设置为executeable1的完整路径 - 但此后在executeable1之称的形式前男友都没有找到......
如何处理这个任何建议是极大的AP preciated ...

 字符串pathvar = System.Environment.GetEnvironmentVariable(PATH);
System.Environment.SetEnvironmentVariable(PATH,pathvar + @; C:\\ UD_ \\ BIN \\ DAYSIM \\ bin_windows \\; C:\\ UD_ \\ BIN \\光辉\\ BIN \\; C:\\ UD_ \\ BIN \\ DAYSIM;) ;
                System.Environment.SetEnvironmentVariable(射线路径,@C:\\ UD_ \\ BIN \\ DAYSIM \\ lib目录\\; C:\\ UD_ \\ BIN \\光辉\\ lib目录\\);的System.Diagnostics.Process P =新的System.Diagnostics.Process();
p.StartInfo.WorkingDirectory = @C:\\ UD_ \\ BIN \\ DAYSIM \\ bin_windows//字符串pathvar = p.StartInfo.EnvironmentVariables [路径];
//p.StartInfo.EnvironmentVariables[\"PATH] = pathvar + @; C:\\ UD_ \\ BIN \\ DAYSIM \\ bin_windows \\; C:\\ UD_ \\ BIN \\光辉\\ BIN \\; C:\\ UD_ \\ BIN \\ DAYSIM ;;
//p.StartInfo.EnvironmentVariables[\"RAYPATH] = @C:\\ UD_ \\ BIN \\ DAYSIM \\ lib目录\\; C:\\ UD_ \\ BIN \\光辉\\ 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 =新的ProcessStartInfo();//设置射线路径变量设置为测试
//新进程将有射线路径变量,测试价值创造
//创建的过程的所有环境变量从继承
//当前进程
startInfo.EnvironmentVariables [射线路径] =测试;//为需要进行设置EnvironmentVariables
startInfo.UseShellExecute = FALSE;//设置一些可执行文件名
在目录指定//可执行必应搜索
//当前进程的PATH变量
startInfo.FileName =cmd.exe的;//启动过程
的Process.Start(StartInfo的);

can someone help me with understanding 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:

  1. I created a process and set the two varables in StartInfo. The variables exist already but are missing the needed information.
  2. I tried to set the variables with System.Environment.SetEnvironmentVariable().

When I run the process the system cant find the executable ("executeable1"). I tried to set StartInfo.FileName to the full path of "executeable1" - however then the exes called form within "executeable1" are not found... Any advise on how to deal with this is greatly appreciated...

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

解决方案

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:

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

这篇关于设置环境变量的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆