ZC: 我的示例代码(Delphi):http://www.cnblogs.com/CodeSkill/p/8341464.html
1、资料:
如何用代码设置环境变量?-CSDN论坛.html(http://bbs.csdn.net/topics/50445308)
如何修改Windows系统环境变量 _ 蓝讯.html(http://www.lxway.com/261806661.htm)
How to propagate environment variables to the system.html(https://support.microsoft.com/en-us/help/104011/how-to-propagate-environment-variables-to-the-system)
1.1、SetEnvironmentVariable --> 无法影响到 系统环境变量(∴ 用它修改的话,在 "???-->系统属性-->环境变量" 的窗口里面是看不到对应的变化的)
1.2、直接修改 注册表 HKEY_LOCAL_MACHINESystemCurrentControlSetControlSession ManagerEnvironment 里面的键值(要想所有窗口都立即知道环境变量改变了的话,就需要广播[下面资料里面有])
ZC: 在 CSDN的帖子里面 提到 HKEY_LOCAL_MACHINESYSTEMControlSet001Session ManagerEnvironmentpath,自己弄了一下,这里改了的话 "环境变量"里面的值也会变。测试了一下:"...CurrentControlSet...Environment"和"...ControlSet001...Environment" 中的值,不管修改哪边的键值,另一边是会跟着变的,但是 保守起见 还是用 MSDN里面官方说的地方吧...
1.3、CSDN的帖子里 还提到一个函数:ExpandEnvironmentStrings,未做测试 也未查看相关资料...
2、MSDN中的资料:(https://support.microsoft.com/en-us/help/104011/how-to-propagate-environment-variables-to-the-system)
How to propagate environment variables to the system
Summary
You can modify system environment variables by editing the following Registry key:
HKEY_CURRENT_USER
Environment
Note that any environment variable that needs to be expanded (for example, when you use %SYSTEM%) must be stored in the registry as a REG_EXPAND_SZ registry value. Any values of type REG_SZ will not be expanded when read from the registry.
HKEY_LOCAL_MACHINE
SYSTEM
CurrentControlSet
Control
Session Manager
Environment
Note that RegEdit.exe does not have a way to add REG_EXPAND_SZ. Use RegEdt32.exe when editing these values manually.
However, note that modifications to the environment variables do not result in immediate change. For example, if you start another Command Prompt after making the changes, the environment variables will reflect the previous (not the current) values. The changes do not take effect until you log off and then log back on.
To effect these changes without having to log off, broadcast a WM_SETTINGCHANGE message to all windows in the system, so that any interested applications (such as Windows Explorer, Program Manager, Task Manager, Control Panel, and so forth) can perform an update.
More Information
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
(LPARAM) "Environment", SMTO_ABORTIFHUNG,
5000, &dwReturnValue);
Last Updated: Jan 8, 2017
3、
4、
5、