在C#中设置环境变量,该变量在执行完成后仍然存在 [英] Setting Environment variables in C# that persist after execution is complete
问题描述
我需要从C#程序中设置环境变量,以便稍后运行的批处理文件可以使用此新创建的变量.我尝试使用:
I need to setup an environment variable from a C# program so batch files that run later can use this newly created variable. I have tried using:
Environment.SetEnvironmentVariable("USRNAM", "My Name",
EnvironmentVariableTarget.Process);
在此语句之后,我设置了一个断点,当它到达该断点时,我转到命令提示符,发出以下命令:
After this statement I have a breakpoint setup and when it gets to this breakpoint, I go to a Command Prompt, issue the following command:
C:\Users\Lenovo>SET USRNAM
我得到:
Environment variable USRNAM not defined.
如何设置在C#程序执行完成后仍然存在的环境变量?
How can I set an Environment variable that persists after the C# program execution has completed?
非常感谢您的建议.
推荐答案
尝试使用 EnvironmentVariableTarget.User
或 EnvironmentVariableTarget.Machine
,具体取决于您是否愿意为当前用户或所有用户创建的变量.
Try to use either EnvironmentVariableTarget.User
or EnvironmentVariableTarget.Machine
, depending on whether you'd like the variable to be created for the current user or for all users.
在此处中查找有关该枚举的信息.
Look here for information on that enumeration.
这篇关于在C#中设置环境变量,该变量在执行完成后仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!