[Tutor] walk registry using _winreg
Albert-Jan Roskam
fomcl at yahoo.com
Wed May 29 10:11:34 CEST 2013
Hello,
I created a program to go through the windows registry and look for a certain key ("file_locations", though in the example I am using a key that every windows user has on his/her computer). If found, I want to replace the data associated with value "temp_dir" in that key. I have chosen this strategy because the exact registry keys may have changed from version to version. Also, multiple versions of the program may be installed on a given computer. I pasted the code below this mail, but also here: http://pastebin.com/TEkyekfi
Is this the correct way to do this? I would actually prefer to specify only "valueNameToSearch" and not also "keyToSearch". As in: start walking through the registry starting at <regkey>, return every key where a temp_dir is defined.
Thank you in advance!
Regards,
Albert-Jan
import _winreg
import os
global __debug__
__debug__ = True
def walkRegistry(regkey, keyToSearch="file_locations",
valueNameToSearch="temp_dir", verbose=False):
"""Recursively search the Windows registry (HKEY_CURRENT_USER),
starting at top <regkey>. Return a list of three tuples that contain
the registry key, the value and the associated data"""
if verbose:
print regkey
aReg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, regkey)
i, keys, founds = 0, [], []
try:
while True:
i += 1
key = _winreg.EnumKey(aReg, i)
keys.append(key)
if key == keyToSearch:
keyx = os.path.join(regkey, key)
aReg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, keyx)
data, type_ = _winreg.QueryValueEx(aReg, valueNameToSearch)
founds.append((keyx, valueNameToSearch, data))
except WindowsError:
for key in keys:
try:
new_regkey = os.path.join(regkey, key)
walkRegistry(new_regkey, keyToSearch,
valueNameToSearch, verbose)
except WindowsError:
pass
return founds
def setRegistry(regkey, keyToSet, valueToSet, replacementData, verbose=False):
"""Search for <keyToSet> starting at top <regkey>. If <keyToSet> is found
exactly once, replace registry data associated with <valueToSet> with
<replacementData>."""
founds = walkRegistry(regkey, keyToSet, valueToSet, verbose)
if not founds:
return
elif len(founds) == 1:
keyx, valueNameToSearch, data = founds[0]
else:
msg = "Registry value %r is found multiple times"
raise ValueError(msg % valueToSet)
if not __debug__:
try:
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, keyx,
0, _winreg.KEY_ALL_ACCESS)
except:
key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, regkey)
_winreg.SetValueEx(keyx, valueToSet, 0, _winreg.REG_SZ,
replacementData)
_winreg.CloseKey(keyx)
regkey = u"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"
print founds = walkRegistry(regkey, u"Shell Folders", u"Cookies", True)
setRegistry(regkey, u"Shell Folders", u"Cookies", "this is the replacement value", False)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the Tutor
mailing list