A:使用SendMessage ,例子如下:
;修改环境变量
WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "abcd" "ggg"
;刷新环境变量
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment"
如何将路径添加到系统环境变量中
NSIS, 系统环境变量
问:比如,我把 a.exe 用nsis包装好,安装到 c:\helloLib\a.exe,完成后,想把c:\helloLib\添加到系统环境变量的path里
头,这样,在任何地方输入a.exe可执行。如何将路径添加到系统环境变量中?
答:以下代码实现:
程序代码
ReadRegStr $0 HKLM “SYSTEM\CurrentControlSet\Control\Session Manager\Environment” “Path”
WriteRegExpandStr HKLM “SYSTEM\CurrentControlSet\Control\Session Manager\Environment” “Path” “$0;C:\hellolib”
复制代码
另类方法一:写注册表,如
程序代码
- [HKEY_CLASSES_ROOT\Applications\a.exe\shell\open\command]
- @=”yourpath\a.exe”
另类方法二:
程序代码
1.[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\a.exe]
2. @=”c:\helloLib\a.exe”
【NSIS使用经验】修改和删除环境变量
;设置oracle的环境变量到PATH路径
ReadRegStr $0 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path"
WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$0;$INSTDIR\Oracle\bin\"
;刷新环境变量
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment"
;利用WordReplace函数从PATH中删除设置oracle的环境变量
;需要在文件头引入相应的头文件:!include “WordFunc.nsh”
ReadRegStr $R0 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path"
${WordReplace} $R0 ";$INSTDIR\Oracle\bin\" "" "+" $R1
;MessageBox MB_OK|MB_USERICON '$R0 - $INSTDIR - $R1 '
WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$R1"