Windows.环境变量(设置)

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_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 里面的键值(要想所有窗口都立即知道环境变量改变了的话,就需要广播[下面资料里面有])

    ZC: 在 CSDN的帖子里面 提到 HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Session Manager\Environment\path,自己弄了一下,这里改了的话 "环境变量"里面的值也会变。测试了一下:"...\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 user environment variables by editing the following Registry key:


HKEY_CURRENT_USER \
Environment
You can modify system environment variables by editing the following Registry key:


HKEY_LOCAL_MACHINE \
SYSTEM \
CurrentControlSet \
Control \
Session Manager \
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.


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


For example, on Windows NT-based systems, the following code fragment should propagate the changes to the environment variables used in the Command Prompt:
   SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
(LPARAM) "Environment", SMTO_ABORTIFHUNG,
5000, &dwReturnValue);
None of the applications that ship with Windows 95 and Windows 98, including Windows Explorer and Program Manager, respond to this message. Thus, while this article can technically be implemented on Windows 95 and Windows 98, there is no effect except to notify third-party applications. The only method of changing global environment variables on Windows 95 is to modify the autoexec.bat file and reboot.
 

Last Updated: Jan 8, 2017
 
 

3、

4、

5、

 

posted @ 2018-01-24 13:40  CppSkill  阅读(411)  评论(0编辑  收藏  举报