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

发布于 2024-07-25 08:34:12 字数 514 浏览 4 评论 0原文

在这种情况下,设置环境变量的应用程序在需要访问 env.var 的应用程序中执行。 Main() 返回值(C# 编程指南) msdn 文章讨论了它在批处理文件中的使用。 如果我也尝试一下,一切都会好起来的; 但需要的是不是从批处理脚本运行,而是从应用程序内部运行。

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

显然是不成功的。 我相信 Process.Start 使“应用程序”在完全不同的环境中工作。 换句话说,我需要在与调用者应用程序相同的环境中运行“app”才能到达它设置的环境变量。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

败给现实 2024-08-01 08:34:12

如果您只是尝试从父进程设置子进程环境:

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

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

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

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);
海未深 2024-08-01 08:34:12

环境变量继承给子进程,但每个子进程都会获得一个副本 - 如果您随后更改父进程的环境,这不会反映在子进程中。

这是出于安全原因:如果共享变量,进程可以查看彼此的内存,这会导致各种问题。

所以解决方案是在开始新进程之前设置变量。

如果需要与现有子进程通信,请使用管道。

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.

塔塔猫 2024-08-01 08:34:12

每个应用程序都以其自己的环境副本运行,因此子进程无法影响父进程的环境。 一直到 CreateProcess 都是如此,其中环境是一个输入/可选参数 - 即单向。

有许多可用的 IPC 机制,从命名管道到套接字,从共享内存到文件……这样的例子不胜枚举。

但我怀疑文件对你来说将是最简单的。

您可以让子进程创建一个文件,其中包含您想要的名称/值对,然后调用应用程序可以加载和使用该文件。 格式可以是基本的,例如:

key=value
key2=value2

有点复杂(但可能更容易使用),例如 XML ...或任何您想要的自定义格式。

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.

There are many IPC mechanisms you have available from named pipes to sockets to shared memory to files ... the list goes on.

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.

七色彩虹 2024-08-01 08:34:12

该命令必须在当前进程的环境中执行。 通常,bash 会将所有进程作为子进程执行,该子进程会获得父环境的只读副本,并在修改变量时创建一个新条目。

点 (.) 是一个命令,不应与当前目录的说明混淆。 点命令导致以下命令在父环境中执行。 这样,进程的环境变量就是调用进程的环境变量。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文