通过这个简单的设置,我可以在注册表中创建任何用户环境变量:
import win32con
import win32gui
import _winreg as winreg
def set_environment_variable(variable, value, user_env=True):
if user_env: reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Environment', 0, winreg.KEY_SET_VALUE)
else: reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 0, winreg.KEY_SET_VALUE)
if '%' in value: var_type = winreg.REG_EXPAND_SZ
else: var_type = winreg.REG_SZ
with reg_key:
winreg.SetValueEx(reg_key, variable, 0, var_type, value)
win32gui.SendMessageTimeout(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment', win32con.SMTO_ABORTIFHUNG, 1000)
创建MY_VARIABLE
:
以下是截图:
问题:如何删除刚刚创建的MY_VARIABLE
?在
您可以使用以下方法轻松地从Windows注册表中设置或删除环境变量:
要设置环境变量:
^{pr2}$要删除环境变量:
您可以通过以下方式导入
win32
库:相关问题 更多 >
编程相关推荐