注册表分两部分:键和值
键是一个树结构,一个键下面可以有很多子键
官网:链接
方法 | 解释 |
RegOpenKey | 打开注册表项 |
RegQueryInfoKey | 检索有关指定注册表项的信息 |
RegQueryValue | 检索有关指定注册表项的信息 |
查询键值
import win32con,win32api
def get_Reg_key(path,key_):
# coding:utf-8
reg_root = win32con.HKEY_LOCAL_MACHINE
#reg_root = win32con.HKEY_CLASSES_ROOT
reg_path = r'%s' % path
reg_flags = win32con.KEY_READ | win32con.KEY_WOW64_64KEY
# 读取键值
key = win32api.RegOpenKey(reg_root, reg_path, 0, reg_flags)
value, key_type = win32api.RegQueryValueEx(key, key_)
#关闭键
win32api.RegCloseKey(key)
return {"value":value}
查询遍历注册表
import win32con,win32api
def get_software_list():
reg_root = win32con.HKEY_LOCAL_MACHINE
reg_flags = win32con.KEY_READ | win32con.KEY_WOW64_64KEY
sub_key = r'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
soft_ware = []
key = win32api.RegOpenKey(reg_root,sub_key,0,reg_flags)
for j in range(0,win32api.RegQueryInfoKey(key)[0]):
try:
key_name = win32api.RegEnumKey(key,j)
key_path = r'%s' % sub_key + '\\' + key_name
path = r'%s' % key_path
each_key = win32api.RegOpenKey(reg_root, path, 0, reg_flags)
try:
display_name,REG_SZ = win32api.RegQueryValueEx(each_key, 'DisplayName')
publisher,REG_SZ = win32api.RegQueryValueEx(each_key, 'Publisher')
#name_exe,REG_SZ= win32api.RegQueryValueEx(each_key,'UninstallString')
if publisher == "Seewo":
block = False
seewoApp = True
elif publisher == 'Microsoft Corporation':
continue
else:
block = True
seewoApp =False
sortware_info = {"block":block,"name":display_name,"seewoApp":seewoApp}
soft_ware.append(sortware_info)
except:
continue
print(soft_ware)
# 关闭键
win32api.RegCloseKey(each_key)
except WindowsError:
pass
win32api.RegCloseKey(key)
return {"soft_ware":soft_ware}
删除注册表里某键值
import win32api,win32con
def del_RegKey(subkey_name):
reg_root = win32con.HKEY_LOCAL_MACHINE
reg_path = r"SOFTWARE\WOW6432Node\Seewo"
reg_flags = win32con.WRITE_OWNER | win32con.KEY_WOW64_64KEY | win32con.KEY_ALL_ACCESS
# 读取键值
key = win32api.RegOpenKey(reg_root, reg_path, 0, reg_flags)
win32api.RegDeleteKey(key, 'test')
# 关闭键
win32api.RegCloseKey(key)
key = win32api.RegOpenKey(reg_root, reg_path, 0, reg_flags)
win32api.RegCloseKey(key)
添加注册表里键值
import win32api,win32con
def reg_add_EasiNote():
reg_root = win32con.HKEY_LOCAL_MACHINE
reg_path = r"SOFTWARE\WOW6432Node\Seewo\EasiNote5"
reg_flags = win32con.WRITE_OWNER | win32con.KEY_WOW64_64KEY | win32con.KEY_ALL_ACCESS
key,_=win32api.RegCreateKeyEx(reg_root, reg_path,reg_flags)
# key = win32api.RegOpenKey(reg_root, reg_path, 0, reg_flags)
win32api.RegSetValueEx(key, "type", 0, win32con.REG_SZ, '1')
win32api.RegCloseKey(key)