57

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
timkado
  • 1,922
  • 2
  • 17
  • 27
  • 2
    You've left out the leading semicolon. – Harry Johnston Jan 28 '13 at 00:57
  • Ok - added the leading semicolon - now "executable1" is found and runs - this is great. However, processes called form within "executable1" still seem to fail... any idea? – timkado Jan 28 '13 at 01:12
  • 1
    Perhaps you should try replacing `executable1` with `cmd.exe` so you can see what the environment looks like? It might give you a clue. – Harry Johnston Jan 28 '13 at 01:28
  • Could you point me to a good explanation what the difference is between System.Environment.SetEnvironmentVariable() and putting them into StartInfo? – timkado Jan 28 '13 at 02:16
  • 1
    Not offhand. The short version: SetEnvironmentVariable changes the environment variables for the current process. These changes are presumably inherited by the child process. StartInfo affects the child process only. It is likely that by setting StartInfo the child process gets *only* the environment variables you include, so if you do it that way you probably need to enumerate the existing environment variables and copy them over, adding, modifying or excluding as necessary. – Harry Johnston Jan 28 '13 at 03:09

2 Answers2

114

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);
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • 4
    In your example code, will the child process have just the single environment variable RAYPATH, or will other environment variables also be inherited? Or, to put it another way, does startInfo.EnvironmentVariables get pre-populated by `new ProcessStartInfo()`? – Harry Johnston Jan 30 '13 at 00:07
  • 14
    @HarryJohnston All environment variables are inherited from the parent process. `new ProcessStartInfo()` populates the dictionary. – ken2k Jan 30 '13 at 09:51
6

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.

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dikchani
  • 91
  • 1
  • 1
  • You write "There are many types of environment variables, like system level and users. It depends on your requirements." <-- looks like there types https://learn.microsoft.com/en-us/dotnet/api/system.environmentvariabletarget?view=net-7.0 user, machine(system), and process – barlop Jan 22 '23 at 18:55