Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment and HKEY_CURRENT_USEREnvironment 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.

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_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment and HKEY_CURRENT_USEREnvironment. So you can in general change registry values under HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment or HKEY_CURRENT_USEREnvironment and send

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

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.

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.

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%.cmd and dell %TEMP%.cmd to update environment variables of the current 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_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Paths and %SystemRoot%System32autoexec.nt file. One will be used for processes created by ShellExecute and ShellExecuteEx (for example Explorer.exe) and another for console applications.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...