且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

应用程序如何访问另一个应用程序设置的环境变量?

更新时间:2023-10-20 13:35:04

如果您只想从父级设置孩子的环境:

If you're just trying to set the child's environment from the parent:

var p = new Process();
p.StartInfo.EnvironmentVariables["TEST_ENV"] = "From parent";
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = @"C:\src\bin\Debug\ChildProc.exe";
p.Start();

如果您不希望子代继承父进程环境:

If you don't want the child to inherit the parent process environment:

var psi = new ProcessStartInfo();
psi.EnvironmentVariables["TEST_ENV"] = "From parent";
psi.UseShellExecute = false;
psi.FileName = @"C:\src\bin\Debug\ChildProc.exe";
Process.Start(psi);