Because the Windows XP API does not support the RegDeleteTree function, the programmer has to query for subkeys of a registry key recursively, before he can delete it. This functions returns all subkeys of a registry key in deleteable order, which means the deepest subkey is the first in the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import win32api
import win32con
def regquerysubkeys(handle, key, keylist = []):
"""
This function returns a recursively generated list of subkeys of a given
key in deletable order, see win32api.RegDeleteKey.
If an unexpected Exception occurs, it is raised!
Parameters
handle: one of the constants from win32con (e.g. win32con.HKEY_CURRENT_USER)
key: a subkey within the handle as a string
keylist: may be left empty, this is just neede for the recursion
Returns
keylist: a list with subkeys in deleteable order
Sample call
regquerysubkeys(win32con.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\")
"""
#get registry handle
reghandle = win32api.RegOpenKeyEx(handle, key, 0, win32con.KEY_READ)
try:
i = 0
#enumerates subkeys and recursively calls this function again
while True:
subkey = win32api.RegEnumKey(reghandle, i)
i += 1
#braintwister here ;-)
regquerysubkeys(handle, key + subkey + "\\", keylist)
except win32api.error as ex:
#If no more subkeys can be found, we can append ourself
if ex[0] == 259:
keylist.append(key)
#unexpected exception is raised
else:
raise
finally:
#do some cleanup and close the handle
win32api.RegCloseKey(reghandle)
#returns the generated list
return keylist
|
Hi,
Thanks for the codes and it was very useful to me! I am a novice in Python, having attended a course few weeks ago and I'm working on my assignment now, and I encounter this issue when I was trying to print the entire Windows Registry (WR) sub-keys directories (trying that first with the below codes with much adapted from yours) and its value (later on).
Kindly advise if any, as it has been taking me few days. What I need is a display on the std screen the entire directory of sub-keys (inserted into a list) in a WR key say, HKEY_CURRENT_FIG, and then I will write the contents into a text or CSV file. Thanks advance!
Here are my codes,
import winreg
def traverse(root, key, list): hKey = winreg.OpenKey(root, key) try: i = 0 while True: strFullSubKey = "" try: strSubKey = winreg.EnumKey(hKey, i) if (key != ""): strFullSubKey = key + "\" + strSubKey else: strFullSubKey = strSubKey except WindowsError: hKey.Close() return traverse(root, strFullSubKey, list) list.append(strFullSubKey) i += 1
global list list = list() traverse (winreg.HKEY_CURRENT_CONFIG,"",list) print (list)
results on screen, which is not very correct, seeing that the 2nd item ("Software") on the list should not be printed/inside in the first place, as it is not as such in the actual directory structure of HKEY_CURRENT_CONFIG
['Software\Fonts', 'Software', 'System\CurrentControlSet\Control\Print\Printers\HP Deskjet F300 Series', 'System\CurrentControlSet\Control\Print\Printers', 'System\CurrentControlSet\Control\Print', 'System\CurrentControlSet\Control\VIDEO', 'System\CurrentControlSet\Control', 'System\CurrentControlSet\SERVICES\TSDDD\DEVICE0', 'System\CurrentControlSet\SERVICES\TSDDD', 'System\CurrentControlSet\SERVICES\VGASAVE\DEVICE0', 'System\CurrentControlSet\SERVICES\VGASAVE', 'System\CurrentControlSet\SERVICES', 'System\CurrentControlSet', 'System']