python 查询注册表中键的对应项

def Judge_Key(key_name,
              reg_root=win32con.HKEY_CURRENT_USER,#根节点  其中的值可以有:HKEY_CLASSES_ROOT、HKEY_CURRENT_USER、HKEY_LOCAL_MACHINE、HKEY_USERS、HKEY_CURRENT_CONFIG
              reg_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",# 键的路径
              ):
    """
    :param key_name: #  要查询的键名
    :param reg_root: # 根节点
        #win32con.HKEY_CURRENT_USER
        #win32con.HKEY_CLASSES_ROOT
        #win32con.HKEY_CURRENT_USER
        #win32con.HKEY_LOCAL_MACHINE
        #win32con.HKEY_USERS
        #win32con.HKEY_CURRENT_CONFIG
    :param reg_path: #  键的路径
    :return:feedback是(0/1/2/3:存在/不存在/权限不足/报错)
    """
    reg_flags = win32con.WRITE_OWNER | win32con.KEY_WOW64_64KEY | win32con.KEY_ALL_ACCESS
    try:
        key = winreg.OpenKey(reg_root, reg_path, 0, reg_flags)
        location, type = winreg.QueryValueEx(key, key_name)
        print("键存在", "location(数据):", location, "type:", type)
        feedback=0
    except FileNotFoundError as e:
        print("键不存在",e)
        feedback =1
    except PermissionError as e:
        print("权限不足",e)
        feedback = 2
    except:
        print("Error")
        feedback = 3
    return feedback
Judge_Key("ctfmon")
posted @ 2021-11-25 09:16  河图s  阅读(194)  评论(0)    收藏  举报