I have been working on this function to read a registry value from Windows. It seems very verbose and repetitive. I could not find a way to refer to the variables imported from winreg other than to type them explicitly. If I could somehow refer to them, this could be cleaned up and shortened greatly. Suggestions?
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)