Fork Me On GitHub

python 设置系统/用户环境变量

 

系统环境变量

winreg.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'

用户环境变量

HKEY_CURRENT_USER\Environment

 

path_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
                              r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 0, winreg.KEY_ALL_ACCESS)
path_value = winreg.QueryValueEx(path_key, 'Path')
winreg.SetValueEx(path_key, "Path", 0, winreg.REG_EXPAND_SZ, '新的环境变量')
# 刷新注册表 更新环境变量 
HWND_BROADCAST
= 0xFFFF
WM_SETTINGCHANGE
= 0x1A
SMTO_ABORTIFHUNG
= 0x0002
result
= ctypes.c_long()
SendMessageTimeoutW
= ctypes.windll.user32.SendMessageTimeoutW
SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE,
0, u'Environment', SMTO_ABORTIFHUNG, 5000, ctypes.byref(result))

 

posted @ 2019-08-12 21:01  coding_the_life  阅读(2232)  评论(0编辑  收藏  举报