改变调用进程的环境变量 [英] Changing environment variables of the calling process

查看:164
本文介绍了改变调用进程的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一次似乎微不足道,但答案一直躲避我现在几天了。

This one seems trivial but the answer has eluded me for a few days now.

我有一个Windows批处理文件,调用C#程序做一个额外的验证,即不能在一个批处理文件来完成。验证完成后,我需要返回一个状态和一个字符串返回到调用shell。

I have a Windows batch file, that calls a C# program to do an extra verification that cannot be done in a batch file. After the verification is complete I need to return a status and a string back to the calling shell.

现在的返回值是微不足道,我的C#控制台应用程序,只需设置一个回报(如果退出代码你会)值。而且我认为该字符串也将是小菜一碟。我试图用定义一个新的shell变量:

Now the return value is trivial and my C# console app simply sets a return value (exit code if you will). And I thought the string will also be a piece of cake. I attempted to define a new shell variable using the:

Environment.SetEnvironmentVariable("ERR", "Some text");

这调用应该(和不)定义的当前进程中的一个shell变量 - 那就是创建的变量非常C#程序。作为C#应用程序终止该值将丢失,并且创造了C#应用程序的外壳一无所知变量。所以...有没有特别使用的电话...在所有...除非也许,如果我创建了一个子进程从C3的应用程序,或许会继承我的变量。

This call should (and does) define a shell variable within the current process - that is the very C# process that created the variable. The value is lost as soon as the C# app terminates and the shell that created the C# app knows nothing about the variable. So... A call with no particular use... At all... Unless perhaps if I created a child process from the C3 app, perhaps it would inherit my variables.

该EnvironmentVariableTarget.Machine和EnvironmentVariableTarget.User目标的SetEnvironmentVariable方法调用没有任何解决的问题,因为只有新创建的过程中会从注册表获取这些新的值。

The EnvironmentVariableTarget.Machine and EnvironmentVariableTarget.User targets for the SetEnvironmentVariable call don't solve the problem either, as only a newly created process will get these new values from the registry.

因此,唯一的工作解决方案,我能想到的是:

So the only working solution I can think of is:


  • 写入stdout

  • 写入文件

  • 编码额外的意义进入返回值

前两个是有点丑,最后一个有其局限性和问题。

The first two are a bit ugly and the last one has its limitations and problems.

任何其他的想法(如何设置父进程shell变量)?也许这样的shell变量的修改是一个安全问题(认为PATH)...

Any other ideas (how to set a shell variable in the parent process)? Maybe such shell variable modifications are a security concern (think PATH)...

感谢,您的时间。

推荐答案

我有同样的问题,因为瑞恩并来到我的脑海里作为唯一的变通是写在错误批处理到设置变量,并调用它从批

I had the same problem as Ryan and the only thing that came to my mind as a work-around was to write a batch in error out to set the variable and to call it from the batch.

ConsoleApplication1.exe中:

ConsoleApplication1.exe:

'put some sensible code here
'put result in variable myResult
Dim myResult As String = Guid.NewGuid().ToString("D").ToUpperInvariant()
Console.WriteLine("Normal output from the consonle app")
Console.Error.WriteLine("@ECHO OFF")
Console.Error.WriteLine("SET zzzResult={0}", myResult)

TEST.CMD(调用批处理):

Test.cmd (the calling batch):

@ECHO OFF
:Jump to folder of batch file
PUSHD %~d0%~p0

:Define a temp file
SET zzzTempFile=%TEMP%\TMP%Random%.CMD

:Call .NET console app
ConsoleApplication1.exe 2>%zzzTempFile%

:Call the generated batch file
CALL %zzzTempFile%

:Clean up temp file
DEL %zzzTempFile%

:Clean up variable
SET zzzTempFile=

:Do something with the result
ECHO Yeah, we finally got it!
ECHO:
ECHO The value is "%zzzResult%".
ECHO:

:Clean up result variable
SET zzzResult=

:Go back to original folder
POPD

这是应该做的伎俩。是的,我知道这是一个古老的职位和瑞安现在解决等问题,而且有可能是还别人在那里有同样的问题...

That should do the trick. And yes, I do know this is an old post and Ryan is solving other issues by now, but there might be still somebody else out there having the same problem...

这篇关于改变调用进程的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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