如何在 Windows 中通过命令行明确修改 PATH 变量 [英] How to modify the PATH variable definitely through the command line in Windows

查看:33
本文介绍了如何在 Windows 中通过命令行明确修改 PATH 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

抱歉回答太长,但不可能简短回答您的问题.

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

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

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\Environment and HKEY_CURRENT_USER\Environment where 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.

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

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

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

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

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

因此,重要的是要了解 没有 无需重新启动计算机即可更新所有进程的环境变量的简单方法.你应该对此有一个清楚的了解,并减少你的问题.

So it is important to understand that there is no simple 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.

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

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

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

If you want to update environment variables of the current cmd.exe inside 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:\BlaBla and then use call %TEMP%\t.cmd and dell %TEMP%\t.cmd to update environment variables of the current cmd.exe.

确切地说,用于构建新创建进程的环境变量的地方更多.这是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths%SystemRoot%\System32\autoexec.nt 文件的子键.一个用于由 ShellExecuteShellExecuteEx(例如 Explorer.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 Paths and %SystemRoot%\System32\autoexec.nt file. One will be used for processes created by ShellExecute and ShellExecuteEx (for example Explorer.exe) and another for console applications.

这篇关于如何在 Windows 中通过命令行明确修改 PATH 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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