应用程序如何访问由另一个应用程序设置的环境变量? [英] How can an application access the environment variable set by another application?

查看:510
本文介绍了应用程序如何访问由另一个应用程序设置的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,该设置环境变量在/从该需要访问env.var应用程序执行的应用。
中的 main()的返回值(C#编程指南) MSDN文章论述了一个批处理文件中使用。如果我尝试同样的,一切都很好;但什么是需要的是不是从一个批处理脚本,但在应用程序中运行。

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.

Process.Start("app","args"); // app sets the env.var.
string envVar = System.Environment.GetEnvironmentVariable("ERRORLEVEL");



显然是不成功的。的Process.Start在一个完全不同的环境,我相信做出的应用程序的工作。换句话说,我需要在相同的环境中,以达到环境变量它设置主叫方运行应用程序应用程序。

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.

推荐答案

如果你只是想设置从父孩子的环境:

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);

这篇关于应用程序如何访问由另一个应用程序设置的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆