Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variables for a process

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:

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

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(); 
like image 628
timkado Avatar asked Jan 28 '13 00:01

timkado


People also ask

How do I set an environment variable in Windows process?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

Do we need to set environment variables for Node js?

You really do not need to set up your own environment to start learning Node. js. Reason is very simple, we already have set up Node.


1 Answers

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); 
like image 189
ken2k Avatar answered Sep 18 '22 00:09

ken2k