简体   繁体   English

如何从注册表中删除Windows用户环境变量

[英]How to delete Windows User Environment Variable from registry

With this simple setup I can create any User Environment variable in the registry: 通过此简单的设置,我可以在注册表中创建任何用户环境变量:

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)

create a MY_VARIABLE : 创建一个MY_VARIABLE

set_environment_variable('MY_VARIABLE', 'MY_VALUE')

Here is the screenshot: 这是屏幕截图:

在此处输入图片说明

Question: How do I delete just created MY_VARIABLE ? 问题:如何删除刚刚创建的MY_VARIABLE

You can easily set or remove an environment variable from the Windows registry with: 您可以使用以下方法轻松地从Windows注册表中设置或删除环境变量:

def set_environment_variable(variable, value, user_env=True):
    """
    Set/Remove Environment variable from windows registry.

    :param variable: Environment Variable Name
    :param value: Environment Variable Value (None to delete)
    :param user_env: if true set in user env instead of in system env
    :return: None
    """
    if user_env:
        # This is for the user's environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_CURRENT_USER,
            'Environment', 0, winreg.KEY_SET_VALUE)
    else:
        # This is for the system environment variables
        reg_key = winreg.OpenKey(
            winreg.HKEY_LOCAL_MACHINE,
            r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
            0, winreg.KEY_SET_VALUE)

    with reg_key:
        if value is None:
            winreg.DeleteValue(reg_key, variable)
        else:
            if '%' in value:
                var_type = winreg.REG_EXPAND_SZ
            else:
                var_type = winreg.REG_SZ
            winreg.SetValueEx(reg_key, variable, 0, var_type, value)

    # notify about environment change
    win32gui.SendMessageTimeout(
        win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0,
        'Environment', win32con.SMTO_ABORTIFHUNG, 1000)

To set the environment variable: 设置环境变量:

set_environment_variable('MY_VARIABLE', 'MY_VALUE)

To remove the environment variable: 要删除环境变量:

set_environment_variable('MY_VARIABLE', None)

You can import the win32 libs with: 您可以使用以下命令导入win32库:

import win32con
import win32gui
try:
    import _winreg as winreg
except ImportError:
    # this has been renamed in python 3
    import winreg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM