How to make changes to Windows system environment variables take effect immediately

After adding or modifying environment variables in "My Computer" -> "Properties" -> "Advanced" -> "Environment Variables", you need to restart the system to make them take effect. Is there any way to make it take effect immediately? Here's one method:

  Take the modification of the environment variable "PATH" as an example. After the modification is completed, enter the DOS command prompt, enter: set PATH=C: , and close the DOS window. Open the DOS window again, enter: echo %PATH%, you can find that the PATH value set in "My Computer" -> "Properties" -> "Advanced" -> "Environment Variables" has taken effect.

  Don't worry that changes in the DOS window will affect the value of the environment variable, the environment variable in the DOS window is just a copy of the Windows environment variable. But modifications to the copy trigger a refresh of the Windows environment variables, which is exactly what we want!

 

Strange, cmd.exe started with win+R->cmd will find that the environment variables set in the computer properties take effect immediately, but the cmd started in other modes has no effect, strange! ! After checking the information, after setting the environment variables in the computer properties, the programs and threads started in the future will take effect, but the programs that used to reside in memory will not work. Some people say that killing explorer.exe and then starting explorer.exe can stimulate the settings. its role.

 

What should be understood here is that when a program starts, the environment variables are copied to the environment where the program is located, and will not be changed by other programs other than the program during the execution of the program. That is to say, suppose we start a cmd program, and then modify the environment variable settings through the control panel, but the environment variables owned by the already started cmd will not be changed. If we start the cmd program after modifying the environment variables, the program will have the new environment variables.
Then the conclusion is obvious: after modifying the environment variable, if the application is affected, then simply restart the application, the modification of the environment variable will be reflected in the program without restarting the computer; however, If a system service is affected, a restart is necessary to reflect changes to the environment variables in the system service (since there is no way to restart the system service manager without restarting the computer).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326996600&siteId=291194637