Win32 Registry Manipulation w/PythonWin?

Bjorn Pettersen BPettersen at NAREX.com
Wed Jul 10 15:37:48 EDT 2002


> Matthias Huening wrote:
> > Tim Daneliuk <tundra at tundraware.com> wrote in
> > news:qmqgga.g31.ln at eskimo.tundraware.com:
> > 
> > 
> >>Does some kind soul out there happen to have a pointer to a 
> >>tutorial/examples on fiddling with the Win32 registry from
PythonWin?

Here's a small example we're using internally to add an entry to a
machine's path variable (without security patches applied, you can do
this from any machine on your network, so consider yourself warned :-)

-- bjorn

import _winreg

def addPathEntry(entry, machine=None):
	HKLM = _winreg.ConnectRegistry(machine,
_winreg.HKEY_LOCAL_MACHINE)
	SYSTEM = _winreg.OpenKey(HKLM, 'SYSTEM')
	CurrentControlSet = _winreg.OpenKey(SYSTEM, 'CurrentControlSet')
	Control = _winreg.OpenKey(CurrentControlSet, 'Control')
	SessionManager = _winreg.OpenKey(Control, 'Session Manager')
	Environment = _winreg.OpenKey(SessionManager, 'Environment')
	path, type = _winreg.QueryValueEx(Environment, 'Path')
	pathlist = path.split(';')

	if entry not in pathlist:
		pathlist.insert(0, entry)
		path = ';'.join(pathlist)
		EnvironmentWrite = _winreg.OpenKey(SessionManager,
'Environment', 0, _winreg.KEY_SET_VALUE)
		_winreg.SetValueEx(EnvironmentWrite, 'Path', 0,
_winreg.REG_EXPAND_SZ, path)
		_winreg.CloseKey(EnvironmentWrite)

	_winreg.CloseKey(Environment)
	_winreg.CloseKey(SessionManager)
	_winreg.CloseKey(Control)
	_winreg.CloseKey(CurrentControlSet)
	_winreg.CloseKey(SYSTEM)
	_winreg.CloseKey(HKLM)





More information about the Python-list mailing list