Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass custom environment variables to System.Diagnostics.Process

I'm working on an app that invokes external processes like so:

ProcessStartInfo startInfo = new ProcessStartInfo(PathToExecutable, Arguments){
     ErrorDialog = false,
     RedirectStandardError = true,
     RedirectStandardOutput = true,
     UseShellExecute = false,
     CreateNoWindow = true,
     WorkingDirectory = WorkingDirectory
 };

using (Process process = new Process()) {
    process.StartInfo = startInfo;

    process.Start();
    process.BeginErrorReadLine();
    process.BeginOutputReadLine();
    process.WaitForExit();

    return process.ExitCode;
}

One of the processes I'm calling depends on an environment variable that I'd rather not require my users to set. Is there any way to modify the environment variables that get sent to the external process? Ideally I'd be able to make them visible only to the process that's running, but if I have to programmatically set them system-wide, I'll settle for that (but, would UAC force me to run as administrator to do that?)

ProcessStartInfo.EnvironmentVariables is read only, so a lot of help that is...

like image 966
Mike Ruhlin Avatar asked Nov 06 '12 16:11

Mike Ruhlin


People also ask

Can you change environment variables for running process?

In general, you can only influence a process's environment variables at the time the process starts up. If you need to communicate a change to a running process, the environment isn't the right tool.

How do I set an environment variable in a process?

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.

How do I set an environment variable in IIS?

Go to your application in IIS and choose Configuration Editor . Choose Applicationhost. config ... in From combobox. Right click on enviromentVariables element, select 'environmentVariables' element , then Edit Items .

Does each process have its own environment variables?

Design. In all Unix and Unix-like systems, as well as on Windows, each process has its own separate set of environment variables. By default, when a process is created, it inherits a duplicate run-time environment of its parent process, except for explicit changes made by the parent when it creates the child.


2 Answers

You can add values to it though.

From MSDN ProcessStartInfo.EnvironmentVariables Property:

Although you cannot set the EnvironmentVariables property, you can modify the StringDictionary returned by the property. For example, the following code adds a TempPath environment variable: myProcess.StartInfo.EnvironmentVariables.Add("TempPath", "C:\\Temp"). You must set the UseShellExecute property to false to start the process after changing the EnvironmentVariables property. If UseShellExecute is true, an InvalidOperationException is thrown when the Start method is called.

like image 66
Austin Salonen Avatar answered Oct 11 '22 02:10

Austin Salonen


You can set environment variables using the indexer:

process.StartInfo.EnvironmentVariables['name'] = value;
like image 34
Lee Avatar answered Oct 11 '22 01:10

Lee