windows 以编程方式修改环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2121795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:48:59  来源:igfitidea点击:

Programmatically modifiy environment variables?

windowsenvironment-variables

提问by Javier

  • Windows 7.
  • It's for my own machine, so it doesn't matter if it requires admin rights or something.
  • Preferably in Python or .NET, but I can learn a bit of Win32 (C/C++) programming if it's necessary.
  • Windows 7的。
  • 这是我自己的机器,所以它是否需要管理员权限或其他东西都没有关系。
  • 最好使用 Python 或 .NET,但如果需要,我可以学习一些 Win32 (C/C++) 编程。

回答by ghostdog74

if you want to permanently set environment variable, you can insert the new value into registry. eg with vbscript, add the path "c:\test" into PATH variable

如果要永久设置环境变量,可以将新值插入注册表。例如,使用 vbscript,将路径“c:\test”添加到 PATH 变量中

Set WshShell = WScript.CreateObject("WScript.Shell")
strReg = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path"
strSetting = WshShell.RegRead(strReg)
strNewSetting = strSetting&";c\test"
WshShell.RegWrite strReg, strNewSetting

So, if you use Python or other languages, you can do the same thing using your language's own api/modules to read and write registry

所以,如果你使用 Python 或其他语言,你可以使用你语言自己的 api/modules 来做同样的事情来读写注册表

回答by kicsit

In C# the following creates a permanent environment variable:

在 C# 中,以下内容创建了一个永久环境变量:

Environment.SetEnvironmentVariable("foo", "bar", EnvironmentVariableTarget.Machine);

回答by MrDrews

or you could try a Windows PowerShell script; PowerShell is installed on Windows 7 by default.

或者您可以尝试使用 Windows PowerShell 脚本;默认情况下,PowerShell 安装在 Windows 7 上。

run powershell.exe

运行powershell.exe

PS C:\> [Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")

Then, for example, from cmd.exe

然后,例如,从 cmd.exe

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\>echo %TestVariable%
Test value.

C:\>

Or (in a new) powershell.exe

或者(在一个新的)powershell.exe

PS C:\> echo $ENV:TestVariable
Test Value.
PS C:\>

check out http://technet.microsoft.com/en-us/library/ff730964.aspx

查看http://technet.microsoft.com/en-us/library/ff730964.aspx

回答by Jeff Martin

For anyone else looking for a quick commandline answer

对于正在寻找快速命令行答案的其他人

SETX is available on windows servers (natively i think - http://technet.microsoft.com/en-us/library/cc755104.aspx)

SETX 在 Windows 服务器上可用(我认为原生 - http://technet.microsoft.com/en-us/library/cc755104.aspx

Its also available in the Windows 7and 8 toolkit.

它也可在Windows 78 工具包中使用

回答by will

Use the Environment class like this:

像这样使用 Environment 类:

Environment.SetEnvironmentVariable("foo", "bar");

回答by t0mm13b

Programmatically modifying environment variables is only for the duration of the program. Have not heard of actually modifying the environment system-wide and making it effective there and then. I do not think that can be done, that would require poking around at privileged level and possibly messing with the core system to achieve that.

以编程方式修改环境变量仅适用于程序的持续时间。还没有听说过在系统范围内实际修改环境并使其有效。我不认为这是可以做到的,这需要在特权级别上进行探索,并且可能会弄乱核心系统才能实现这一点。

Even under Unix, it cannot be done despite some hacks to achieve it. I do remember seeing code that actually did modify the environment variables under MSDOS, by altering the MSDOS's _psp environment data structure, but that was a single-tasking system and 16bit with no protection whatsoever.

即使在 Unix 下,它也无法完成,尽管有一些技巧可以实现它。我确实记得看到通过改变 MSDOS 的 _psp 环境数据结构实际上确实修改了 MSDOS 下的环境变量的代码,但那是一个单任务系统和 16 位,没有任何保护。

To sum up, I do not think you can and it would be unwise to do so, it could be perceived as if the system is under a threat by a 'trojan' or a 'virus' as a result if attempting to do so, not alone that, as a user, I would not like for a program to modify the system environment variable without my consent! Sure, a program can write to the registry to make it permanent, but I would still like to know what is the purpose of it and why.

总而言之,我认为您不能这样做,而且这样做是不明智的,如果尝试这样做,可能会认为系统受到“木马”或“病毒”的威胁,不仅如此,作为用户,我不希望程序在未经我同意的情况下修改系统环境变量!当然,程序可以写入注册表以使其永久化,但我仍然想知道它的目的是什么以及为什么。