我一直在研究这个函数来从 Windows 读取注册表值。它似乎非常冗长和重复。除了显式键入它们之外,我找不到引用从 winreg 导入的变量的方法。如果我能以某种方式引用它们,这可以被清理并大大缩短。建议?
from winreg import *
import winreg
from tkinter import *
from tkinter import messagebox
# to read the (Default) value of a key, put a '.' at the end of the KeyString!
def mymessagebox(messagestr):
root=Tk()
root.withdraw()
messagebox.showwarning('Warning!', messagestr)
def ReadRegValue(KeyString):
try:
KeyRoot, KeyFolder = KeyString.split('\\',1)
if KeyFolder == '.':
KeyFolder = ''
Key = ''
else:
try:
KeyFolder, Key = KeyFolder.rsplit('\\',1)
if Key == '.':
Key = ''
except ValueError:
Key = ''
if Key == '':
if KeyRoot == 'HKEY_CLASSES_ROOT':
try:
keyhandle = winreg.ConnectRegistry(None, HKEY_CLASSES_ROOT)
keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
return keyvalue
except FileNotFoundError:
mymessagebox(KeyFolder + ' not found in registry')
elif KeyRoot == 'HKEY_CURRENT_USER':
try:
keyhandle = winreg.ConnectRegistry(None, HKEY_CURRENT_USER)
keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
return keyvalue
except FileNotFoundError:
mymessagebox(KeyFolder + ' not found in registry')
elif KeyRoot == 'HKEY_LOCAL_MACHINE':
try:
keyhandle = winreg.ConnectRegistry(None, HKEY_LOCAL_MACHINE)
keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
return keyvalue
except FileNotFoundError:
mymessagebox(KeyFolder + ' not found in registry')
elif KeyRoot == 'HKEY_USERS':
try:
keyhandle = winreg.ConnectRegistry(None, HKEY_USERS)
keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
return keyvalue
except FileNotFoundError:
mymessagebox(KeyFolder + ' not found in registry')
elif KeyRoot == 'HKEY_PERFORMANCE_DATA':
try:
keyhandle = winreg.ConnectRegistry(None, HKEY_PERFORMANCE_DATA)
keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
return keyvalue
except FileNotFoundError:
mymessagebox(KeyFolder + ' not found in registry')
elif KeyRoot == 'HKEY_CURRENT_CONFIG':
try:
keyhandle = winreg.ConnectRegistry(None, HKEY_CURRENT_CONFIG)
keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
return keyvalue
except FileNotFoundError:
mymessagebox(KeyFolder + ' not found in registry')
elif KeyRoot == 'HKEY_DYN_DATA':
try:
keyhandle = winreg.ConnectRegistry(None, HKEY_DYN_DATA)
keyvalue = winreg.QueryValue(keyhandle, KeyFolder)
return keyvalue
except FileNotFoundError:
mymessagebox(KeyFolder + ' not found in registry')
else:
mymessagebox(KeyRoot + ' is not a valid Key Root')
else:
if KeyRoot == 'HKEY_CLASSES_ROOT':
try:
keyhandle = winreg.OpenKey(HKEY_CLASSES_ROOT, KeyFolder, 0, KEY_READ)
keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
return keyvalue
except FileNotFoundError:
mymessagebox(Key + ' not found in registry')
elif KeyRoot == 'HKEY_CURRENT_USER':
try:
keyhandle = winreg.OpenKey(HKEY_CURRENT_USER, KeyFolder, 0, KEY_READ)
keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
return keyvalue
except FileNotFoundError:
mymessagebox(Key + ' not found in registry')
elif KeyRoot == 'HKEY_LOCAL_MACHINE':
try:
keyhandle = winreg.OpenKey(HKEY_LOCAL_MACHINE, KeyFolder, 0, KEY_READ)
keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
return keyvalue
except FileNotFoundError:
mymessagebox(Key + ' not found in registry')
elif KeyRoot == 'HKEY_USERS':
try:
keyhandle = winreg.OpenKey(HKEY_USERS, KeyFolder, 0, KEY_READ)
keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
return keyvalue
except FileNotFoundError:
mymessagebox(Key + ' not found in registry')
elif KeyRoot == 'HKEY_PERFORMANCE_DATA':
try:
keyhandle = winreg.OpenKey(HKEY_PERFORMANCE_DATA, KeyFolder, 0, KEY_READ)
keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
return keyvalue
except FileNotFoundError:
mymessagebox(Key + ' not found in registry')
elif KeyRoot == 'HKEY_CURRENT_CONFIG':
try:
keyhandle = winreg.OpenKey(HKEY_CURRENT_CONFIG, KeyFolder, 0, KEY_READ)
keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
return keyvalue
except FileNotFoundError:
mymessagebox(Key + ' not found in registry')
elif KeyRoot == 'HKEY_DYN_DATA':
try:
keyhandle = winreg.OpenKey(HKEY_DYN_DATA, KeyFolder, 0, KEY_READ)
keyvalue, keytype = winreg.QueryValueEx(keyhandle, Key)
return keyvalue
except FileNotFoundError:
mymessagebox(Key + ' not found in registry')
else:
mymessagebox(KeyRoot + ' is not a valid Key Root')
except ValueError:
mymessagebox('Your key string Must be in the format SOME_VALID_ROOT_KEY\folder\key . To show a (Default) key value, put a "." at the end of your string.')
#
# Lets do some tests...
#
results = ReadRegValue(r'HKEY_CLASSES_ROOT\.')
print (results)