Wednesday, May 04, 2005

Manipulating Windows registry values

Here is a small class that connects to the Windows registry and creates sub-keys and/or values for a given registry key. I used this functionality very recently to add a directory to the PATH environment variable by modifying the Environment\Path registry value.

Update 06/08/05: When modifying the 'Path' registry value, we need to set a value type of REG_EXPAND_SZ and not REG_SZ, otherwise variables like %SystemRoot% will not get correctly expanded by the command prompt interpreter and the WINDOWS\system32 directory will not be found. I added a value_type parameter defaulting to REG_SZ to the set_value method.
import _winreg
class WinRegistry:

def __init__(self, reg=_winreg.HKEY_LOCAL_MACHINE,
key=r"System\CurrentControlSet\Control\Session Manager\Environment"):
self.reg = _winreg.ConnectRegistry(None, reg)
self.key = key

def create_subkey(self, subkey):
key = self.key + "\\" + subkey
_winreg.CreateKey(self.reg, key)

def delete_subkey(self, subkey):
key = self.key + "\\" + subkey
_winreg.DeleteKey(self.reg, key)

def set_value(self, value_name="", value="", subkey=None, value_type=_winreg.REG_SZ):
key = self.key
if subkey:
key += "\\" + subkey
k = _winreg.OpenKey(self.reg, key, 0, _winreg.KEY_WRITE)
_winreg.SetValueEx(k, value_name, 0, value_type, value)
_winreg.CloseKey(k)

def get_value(self, value_name="", subkey=None):
key = self.key
if subkey:
key += "\\" + subkey
k = _winreg.OpenKey(self.reg, key)
value = _winreg.QueryValueEx(k, value_name)[0]
_winreg.CloseKey(k)
return value

def delete_value(self, value_name="", subkey=None):
key = self.key
if subkey:
key += "\\" + subkey
k = _winreg.OpenKey(self.reg, key, 0, _winreg.KEY_WRITE)
_winreg.DeleteValue(k, value_name)
_winreg.CloseKey(k)

if __name__ == '__main__':
wr = WinRegistry()

# Add ApplicationPath1 value_name to the Environment key
value_name="ApplicationPath1"
value="C:\\agent1\\agent.bat"
wr.set_value(value_name, value, value_type=_winreg.REG_EXPAND_SZ)
value = wr.get_value(value_name)
print "Created value_name %s with value %s" % (value_name, value)
# Now delete value_name
print "Now deleting", value_name
wr.delete_value(value_name)

# Create subkey Environment\EnvTest1 and add ApplicationPath2 value_name to it
subkey = "EnvTest1"
value_name="ApplicationPath2"
value="C:\\agent2\\agent.bat"
wr.create_subkey(subkey)
print "Created subkey", subkey
wr.set_value(value_name, value, subkey)
value = wr.get_value(value_name, subkey)
print "Created value_name %s with value %s" % (value_name, value)
# Now delete subkey
print "Now deleting", subkey
wr.delete_subkey(subkey)

0 Comments:

Post a Comment

<< Home