编写环境变量并从同一个过程中读取它

时间:2021-10-01 10:35:28

I am trying to set environment variable from a console application by executing it from my windows application. I invoke console application and send the value of environment variable as parameter to it, then set the thread to wait for 10 seconds to proceed with next execution. In the next step i try to load a new .exe which reads the value set to the environment variable. The exe will not read the new value and continue to refer the value set earlier.

我试图通过从我的Windows应用程序执行它来从控制台应用程序设置环境变量。我调用控制台应用程序并将环境变量的值作为参数发送给它,然后将线程设置为等待10秒以继续下一次执行。在下一步中,我尝试加载一个新的.exe,它读取设置为环境变量的值。 exe不会读取新值并继续引用之前设置的值。

Once the solution of application is closed and open then it reads the new value i.e reload the vshost.

一旦应用程序的解决方案关闭并打开,它就会读取新值,即重新加载vshost。

1 个解决方案

#1


1  

betting you set up the variable only for the current process. You should try this overload of the Environment.SetEnvironmentVariable method :

打赌你只为当前进程设置变量。您应该尝试Environment.SetEnvironmentVariable方法的这个重载:

Environment.SetEnvironmentVariable("YourVar", "YourValue", 
    EnvironmentVariableTarget.User);

[Edit] Re-Reading your question, you said in the title "same process", and in the question "new exe". In term of Env varialble, spanning a new process implies a new process scope for env variables. They won't share env variables with process scope just because it's the same executable. Maybe you should explain what you are trying to do at a higher level.

[编辑]重新阅读你的问题,你在标题“同一过程”和问题“新的exe”中说。就Env变量而言,跨越新过程意味着env变量的新过程范围。他们不会仅仅因为它是相同的可执行文件而与进程范围共享env变量。也许你应该解释一下你想要在更高层次上做什么。

[Edit2] not sure to understand why it fails... But you can specify env variable when spawning process using a ProcessStartInfo.EnvironmentVariables Property

[Edit2]不确定为什么它会失败...但是你可以在使用ProcessStartInfo.EnvironmentVariables属性生成进程时指定env变量

Basically, it can be (not tested) :

基本上,它可以(未经测试):

var psi = new ProcessStartInfo {
    FileName="yourExe"
};
psi.EnvironmentVariables.Add("YourVariable","YourValue");
var process = Process.Start(psi);

#1


1  

betting you set up the variable only for the current process. You should try this overload of the Environment.SetEnvironmentVariable method :

打赌你只为当前进程设置变量。您应该尝试Environment.SetEnvironmentVariable方法的这个重载:

Environment.SetEnvironmentVariable("YourVar", "YourValue", 
    EnvironmentVariableTarget.User);

[Edit] Re-Reading your question, you said in the title "same process", and in the question "new exe". In term of Env varialble, spanning a new process implies a new process scope for env variables. They won't share env variables with process scope just because it's the same executable. Maybe you should explain what you are trying to do at a higher level.

[编辑]重新阅读你的问题,你在标题“同一过程”和问题“新的exe”中说。就Env变量而言,跨越新过程意味着env变量的新过程范围。他们不会仅仅因为它是相同的可执行文件而与进程范围共享env变量。也许你应该解释一下你想要在更高层次上做什么。

[Edit2] not sure to understand why it fails... But you can specify env variable when spawning process using a ProcessStartInfo.EnvironmentVariables Property

[Edit2]不确定为什么它会失败...但是你可以在使用ProcessStartInfo.EnvironmentVariables属性生成进程时指定env变量

Basically, it can be (not tested) :

基本上,它可以(未经测试):

var psi = new ProcessStartInfo {
    FileName="yourExe"
};
psi.EnvironmentVariables.Add("YourVariable","YourValue");
var process = Process.Start(psi);