pass custom environment variables to System.Diagnostics.Process

Posted by Mike Ruhlin on Stack Overflow See other posts from Stack Overflow or by Mike Ruhlin
Published on 2012-11-06T16:56:29Z Indexed on 2012/11/06 16:59 UTC
Read the original article Hit count: 151

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET