In this case the application which sets the environment variable is executed in/from the application that needs to access the env.var. The Main() Return Values (C# Programming Guide) msdn article discusses its use within a batch file. If I try the same, everything is fine; but what is required is to run not from a batch script but from within an application.
在这种情况下,设置环境变量的应用程序在需要访问env.var的应用程序中执行。 Main()返回值(C#编程指南)msdn文章讨论了它在批处理文件中的使用。如果我尝试一样,一切都很好;但是,不需要从批处理脚本运行,而是从应用程序内运行。
Process.Start("app","args"); // app sets the env.var.
string envVar = System.Environment.GetEnvironmentVariable("ERRORLEVEL");
was obviously unsuccessful. Process.Start made the "app" work in a completely different environment I believe. In other words, I need to run "app" in the same environment as the caller application in order to reach the environment variable it sets.
显然是不成功的。 Process.Start使“app”在我认为完全不同的环境中工作。换句话说,我需要在与调用者应用程序相同的环境中运行“app”,以便访问它设置的环境变量。
4 个解决方案
#1
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);
#2
The environment variables are inherited to child processes but each child gets a copy - if you change the parent's environment afterwards, this will not reflect in the child.
环境变量将继承到子进程,但每个子进程都会获得一个副本 - 如果之后更改了父进程的环境,则不会反映在子进程中。
This is for security reasons: If the variables were shared, processes could see into each other's memory which would cause all kinds of problems.
这是出于安全原因:如果变量是共享的,那么进程可以看到彼此的内存,这会导致各种问题。
So the solution is to set the variable before starting the new process.
所以解决方案是在开始新进程之前设置变量。
If you need to communicate with an existing child process, use a pipe.
如果需要与现有子进程通信,请使用管道。
#3
Each application runs with it's own copy of the environment so a child process cannot influence the environment of the parent. This is true all the way down to CreateProcess where the environment is an input/optional parameter - i.e. one-way.
每个应用程序都使用它自己的环境副本运行,因此子进程不会影响父进程的环境。这一直是CreateProcess,其中环境是输入/可选参数 - 即单向。
There are many IPC mechanisms you have available from named pipes to sockets to shared memory to files ... the list goes on.
从命名管道到套接字到共享内存到文件,有许多IPC机制可用......列表继续。
But the suspect that files are going to be the easiest for you.
但怀疑文件对你来说是最容易的。
You could have the child process create a file that contains the name/value pairs you want which the calling application could then load and use. The format could be something basic like:
您可以让子进程创建一个文件,其中包含您希望调用的应用程序可以加载和使用的名称/值对。格式可能是基本的,如:
key=value key2=value2
a bit more complex (but maybe easier to work with) like XML ... or any custom format you want.
更复杂(但可能更容易使用),如XML ...或任何您想要的自定义格式。
#4
The command must be executed in within the environment of the current process. Normally, bash will execute all processes as a child process which is given a read-only copy of the parent's environment and creates a new entry whenever a variable is modified.
该命令必须在当前进程的环境中执行。通常,bash将作为子进程执行所有进程,该进程被赋予父对象环境的只读副本,并在修改变量时创建新条目。
Dot (.) is a command which should not be confused with the specification of the current directory. The dot command causes the following command to be executed within the environment of the parent. In this manner, the environment variables of the process are the environment variables of the calling process.
Dot(。)是一个不应该与当前目录的规范混淆的命令。 dot命令使以下命令在父环境中执行。以这种方式,进程的环境变量是调用进程的环境变量。
#1
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);
#2
The environment variables are inherited to child processes but each child gets a copy - if you change the parent's environment afterwards, this will not reflect in the child.
环境变量将继承到子进程,但每个子进程都会获得一个副本 - 如果之后更改了父进程的环境,则不会反映在子进程中。
This is for security reasons: If the variables were shared, processes could see into each other's memory which would cause all kinds of problems.
这是出于安全原因:如果变量是共享的,那么进程可以看到彼此的内存,这会导致各种问题。
So the solution is to set the variable before starting the new process.
所以解决方案是在开始新进程之前设置变量。
If you need to communicate with an existing child process, use a pipe.
如果需要与现有子进程通信,请使用管道。
#3
Each application runs with it's own copy of the environment so a child process cannot influence the environment of the parent. This is true all the way down to CreateProcess where the environment is an input/optional parameter - i.e. one-way.
每个应用程序都使用它自己的环境副本运行,因此子进程不会影响父进程的环境。这一直是CreateProcess,其中环境是输入/可选参数 - 即单向。
There are many IPC mechanisms you have available from named pipes to sockets to shared memory to files ... the list goes on.
从命名管道到套接字到共享内存到文件,有许多IPC机制可用......列表继续。
But the suspect that files are going to be the easiest for you.
但怀疑文件对你来说是最容易的。
You could have the child process create a file that contains the name/value pairs you want which the calling application could then load and use. The format could be something basic like:
您可以让子进程创建一个文件,其中包含您希望调用的应用程序可以加载和使用的名称/值对。格式可能是基本的,如:
key=value key2=value2
a bit more complex (but maybe easier to work with) like XML ... or any custom format you want.
更复杂(但可能更容易使用),如XML ...或任何您想要的自定义格式。
#4
The command must be executed in within the environment of the current process. Normally, bash will execute all processes as a child process which is given a read-only copy of the parent's environment and creates a new entry whenever a variable is modified.
该命令必须在当前进程的环境中执行。通常,bash将作为子进程执行所有进程,该进程被赋予父对象环境的只读副本,并在修改变量时创建新条目。
Dot (.) is a command which should not be confused with the specification of the current directory. The dot command causes the following command to be executed within the environment of the parent. In this manner, the environment variables of the process are the environment variables of the calling process.
Dot(。)是一个不应该与当前目录的规范混淆的命令。 dot命令使以下命令在父环境中执行。以这种方式,进程的环境变量是调用进程的环境变量。