如何在 Windows 中通过命令行明确修改 PATH 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3636055/
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 15:11:04  来源:igfitidea点击:

How to modify the PATH variable definitely through the command line in Windows

windowssystem

提问by madewulf

I would like to make a .bat file that would add some string at the end of the value of the Windows PATH variable. Warning, I want this change to be definitive, not working only for the current session.

我想制作一个 .bat 文件,它会在 Windows PATH 变量的值的末尾添加一些字符串。警告,我希望此更改是确定的,而不是仅适用于当前会话。

Does somebody know of a way to do this ? As much as possible it should not be dependent on the version of Windows

有人知道这样做的方法吗?尽可能不依赖于 Windows 的版本

回答by Oleg

Sorry for the long answer, but a short answer on your question is impossible.

抱歉回答太长,但对您的问题进行简短回答是不可能的。

First of all you should understand how environment variables works. There are some places in the registry like HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environmentand HKEY_CURRENT_USER\Environmentwhere the environment variables will be hold. On start-up the operation system reads this registry keys. Then one windows process creates another windows process. The parent process can give to the client process any set of environment variables. If the parent process doesn't do this, the child process inherits environment variables of the parent processes.

首先,您应该了解环境变量的工作原理。有像注册表中一些地方HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\EnvironmentHKEY_CURRENT_USER\Environment这里的环境变量都不变。启动时,操作系统会读取此注册表项。然后一个 windows 进程创建另一个 windows 进程。父进程可以为客户端进程提供任何环境变量集。如果父进程不这样做,子进程将继承父进程的环境变量。

To be able update environment variables of a running process with respect of WM_WININICHANGEor WM_SETTINGCHANGEmessages. A windows application caninterpret this messages and reread the current environment variables from the registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environmentand HKEY_CURRENT_USER\Environment. So you can in general change registry values under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environmentor HKEY_CURRENT_USER\Environmentand send

能够根据WM_WININICHANGEWM_SETTINGCHANGE消息更新正在运行的进程的环境变量。Windows 应用程序可以解释此消息并从注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\EnvironmentHKEY_CURRENT_USER\Environment. 因此,您通常可以更改HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment或下的注册表值HKEY_CURRENT_USER\Environment并发送

SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment");

It would be much better to use SendMessageTimeoutinstead of SendMessage, but the idea will stay the same. The problem is that other processes must notwait for the message and do something. Most console application have no message loop and don't do anything if you send such messages.

使用SendMessageTimeout而不是SendMessage会好得多,但想法将保持不变。问题是其他进程不能等待消息而做某事。大多数控制台应用程序没有消息循环,如果您发送此类消息,则不会执行任何操作。

So it is important to understand that there is nosimple way to update environment variables of all processes without restarting of the computer. You should have a clear understanding of this and reduce your question a little.

因此,重要的是要了解没有简单的方法可以在不重新启动计算机的情况下更新所有进程的环境变量。你应该对此有一个清楚的了解,并减少你的问题。

If you update the environment in registry and send SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment")then new processed created by Explorer.exewill be have new environment variables, but cmd.exewill not do this.

如果您更新注册表中的环境并发送,SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment")那么由Explorer.exe创建的新处理将具有新的环境变量,但cmd.exe不会这样做。

If you want to update environment variables of the current cmd.exeinside a batch run you can do the following: You can create a new CMD file for example t.cmd in %TEMP% directory, write in the file SET PATH=%PATH%;C:\BlaBlaand then use call %TEMP%\t.cmdand dell %TEMP%\t.cmdto update environment variables of the current cmd.exe.

如果您想在当前的更新环境变量cmd.exe的批处理中运行,你可以做到以下几点:您可以创建例如t.cmd在%TEMP%目录下,文件中写入一个新的CMD文件SET PATH=%PATH%;C:\BlaBla,然后使用call %TEMP%\t.cmddell %TEMP%\t.cmd来更新当前cmd.exe 的环境变量。

To be exactly there are more places which are used to build environment variables of new created processes. This are subkeys of the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Pathsand %SystemRoot%\System32\autoexec.ntfile. One will be used for processes created by ShellExecuteand ShellExecuteEx(for example Explorer.exe) and another for console applications.

确切地说,有更多的地方用于构建新创建的进程的环境变量。这是密钥HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths%SystemRoot%\System32\autoexec.nt文件的子密钥。一个用于由ShellExecuteand ShellExecuteEx(例如 Explorer.exe)创建的进程,另一个用于控制台应用程序。

回答by jrtipton

If you only care about new process instances, and you really want it to be done via a batch file, then setxis what you're looking for.

如果您只关心新流程实例,并且确实希望通过批处理文件来完成,那么setx这就是您要寻找的。

  • /Mwill change the PATHin HKEY_LOCAL_MACHINEinstead of HKEY_CURRENT_USER.
    i.e. a system variable, instead of user's.
    example: SETX /M PATH "%PATH%;C:\your path with spaces"
  • /M将更改PATHinHKEY_LOCAL_MACHINE而不是HKEY_CURRENT_USER
    即系统变量,而不是用户的。
    例子:SETX /M PATH "%PATH%;C:\your path with spaces"

If you want to directlychange an environment variable for currently running processes, well, yeah, that's complicated and apparently not recommended:

如果您想直接更改当前正在运行的进程的环境变量,嗯,是的,这很复杂,显然不推荐

Altering the environment variables of a child process during process creation 
is the only way one process can directly change the environment variables of 
another process. A process can never directly change the environment variables 
of another process that is not a child of that process.

Otherwise, like Oleg says, programmatically the best way is to change the registry and send WM_SETTINGCHANGEand hope the apps are nice enough to pick it up.

否则,就像 Oleg 所说的那样,以编程方式最好的方法是更改​​注册表并发送WM_SETTINGCHANGE并希望应用程序足够好以接收它。