Scroll through the values ​​or registry key .. _winreg Python

How would I go through all the values ​​of the Windows registry key using the Python _winreg module. I have code that will do what I want, but it relates to sub-sections of the specified registry key.


Here is the code:

from _winreg import * t = OpenKey(HKEY_CURRENT_USER, r"PATH TO KEY", 0, KEY_ALL_ACCESS) try: i = 0 while True: subkey = EnumValue(t, i) print subkey i += 1 except WindowsError: # WindowsError: [Errno 259] No more data is available pass 

Oh, got it. But, if someone knows of another way to do this, I will still agree with this answer!

+7
python windows enumeration registry winreg
source share
4 answers

If EnumValue doesn't help here

 # list all values for a key try: count = 0 while 1: name, value, type = _winreg.EnumValue(t, count) print repr(name), count = count + 1 except WindowsError: pass 
+5
source share

I prefer to avoid mistakes instead of diving right into it ...

Use _winreg.QueryInfoKey to get the number of values:

 import _winreg key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r'PATH\TO\KEY', 0, _winreg.KEY_READ) for i in xrange(0, _winreg.QueryInfoKey(key)[1]): print _winreg.EnumValue(key, i) 

To get the number of keys, the same method, another index (second half of the original question):

 for i in xrange(0, _winreg.QueryInfoKey(key)[0]): print _winreg.EnumKey(key, i) 

Note: use import instead of from ... import to make it explicit where the functions and variables come from. It’s easier to follow the code in the future.

+8
source share

To enumerate registry keys and values, you will need EnumKey() and EnumValue() from the _winreg module. Note that these two methods take an index as an argument and provide you with a key (or value) for that index only. Therefore, in order to get all the keys (or values), you need to increase the index by one and continue until WindowsError .

This post may help you for a detailed understanding of the same. A link to the Github code can be found in the post.

0
source share

for python 3

 import winreg hKey = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, "Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache") try: count = 0 while 1: name, value, type = winreg.EnumValue(hKey, count) print (name), count = count + 1 except WindowsError as err: print(err) pass 
0
source share

All Articles