def OutlookPath():
"""Function to retrieve the path to Outlook from the registry"""
aReg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
aKey = winreg.OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE")
# prepend unused variables with "dummy_" to keep PyLint happy
dummy_n, v, dummy_t = winreg.EnumValue(aKey, 0)
winreg.CloseKey(aKey)
winreg.CloseKey(aReg)
return v
python类HKEY_LOCAL_MACHINE的实例源码
def _get_msbuild(self):
# 14.0 = VS2015
# 12.0 = VS2012
# 4.0 = VS2008
chk = ['14.0', '12.0', '4.0']
p = None
for c in chk:
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\MSBuild\ToolsVersions\{}'.format(c), r'MSBuildToolsPath')
if p is not None:
break
return os.path.join(p[0], 'MSBuild.exe') if p is not None else None
def _get_visual_studio_path(self):
chk = ['14.0', '12.0', '4.0']
p = None
for c in chk:
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\VisualStudio\{}'.format(c), r'ShellFolder')
if p is not None:
break
return p[0] if p is not None else None
def _get_perl(self):
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\perl', 'BinDir')
return p[0] if p is not None else None
def _get_nsis(self):
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\NSIS\Unicode', '')
if p is None:
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\NSIS', '')
return os.path.join(p[0], 'makensis.exe') if p is not None else None
def _get_msbuild(self):
# 14.0 = VS2015
# 12.0 = VS2012
# 4.0 = VS2008
chk = ['14.0', '12.0', '4.0']
p = None
for c in chk:
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\MSBuild\ToolsVersions\{}'.format(c), r'MSBuildToolsPath')
if p is not None:
break
return os.path.join(p[0], 'MSBuild.exe') if p is not None else None
def _get_perl(self):
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\perl', 'BinDir')
return p[0] if p is not None else None
def _get_nsis(self):
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\NSIS\Unicode', '')
if p is None:
p = self._winreg_read(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\NSIS', '')
return os.path.join(p[0], 'makensis.exe') if p is not None else None
def _create_win(self):
try:
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows NT\CurrentVersion\Fonts')
except EnvironmentError:
try:
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows\CurrentVersion\Fonts')
except EnvironmentError:
raise FontNotFound('Can\'t open Windows font registry key')
try:
path = self._lookup_win(key, self.font_name, STYLES['NORMAL'], True)
self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
path = self._lookup_win(key, self.font_name, STYLES[style])
if path:
self.fonts[style] = ImageFont.truetype(path, self.font_size)
else:
if style == 'BOLDITALIC':
self.fonts[style] = self.fonts['BOLD']
else:
self.fonts[style] = self.fonts['NORMAL']
finally:
_winreg.CloseKey(key)
def getWindowsEnvironmentKey(scope):
assert scope in ('user', 'system')
root = winreg.HKEY_CURRENT_USER
subkey = 'Environment'
if scope != 'user':
root = winreg.HKEY_LOCAL_MACHINE
subkey = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
return (root, subkey)
def _create_win(self):
try:
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows NT\CurrentVersion\Fonts')
except EnvironmentError:
try:
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows\CurrentVersion\Fonts')
except EnvironmentError:
raise FontNotFound('Can\'t open Windows font registry key')
try:
path = self._lookup_win(key, self.font_name, STYLES['NORMAL'], True)
self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
path = self._lookup_win(key, self.font_name, STYLES[style])
if path:
self.fonts[style] = ImageFont.truetype(path, self.font_size)
else:
if style == 'BOLDITALIC':
self.fonts[style] = self.fonts['BOLD']
else:
self.fonts[style] = self.fonts['NORMAL']
finally:
_winreg.CloseKey(key)
def _create_win(self):
try:
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows NT\CurrentVersion\Fonts')
except EnvironmentError:
try:
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows\CurrentVersion\Fonts')
except EnvironmentError:
raise FontNotFound('Can\'t open Windows font registry key')
try:
path = self._lookup_win(key, self.font_name, STYLES['NORMAL'], True)
self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
path = self._lookup_win(key, self.font_name, STYLES[style])
if path:
self.fonts[style] = ImageFont.truetype(path, self.font_size)
else:
if style == 'BOLDITALIC':
self.fonts[style] = self.fonts['BOLD']
else:
self.fonts[style] = self.fonts['NORMAL']
finally:
_winreg.CloseKey(key)
def walk_registry(hkey, path, access_flags, keywords, onerror=None):
""" Walks all keys of the registry searching for values that match any of the provided 'keywords'. """
try:
key = winreg.OpenKey(hkey, path, access_flags)
except OSError as e:
if onerror is not None:
onerror(e)
return
i = 0
sub_keys = []
with key:
while True:
try:
sub_keys.append(winreg.EnumKey(key, i))
except OSError:
break
i += 1
i = 0
while True:
try:
data = winreg.EnumValue(key, i)
i += 1
for keyword in keywords:
if keyword.lower() in str(data[0]).lower():
if hkey == winreg.HKEY_LOCAL_MACHINE:
hive = 'HKLM\\'
else:
hive = 'HKCU\\'
print('{0}\\{1}\\{2} = {3}'.format(hive, path, data[0], data[1]))
except OSError:
break
for key in sub_keys:
next_path = os.path.join(path, key)
for item in walk_registry(hkey, next_path, access_flags, keywords, onerror):
yield item
def _create_win(self):
try:
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows NT\CurrentVersion\Fonts')
except EnvironmentError:
try:
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,
r'Software\Microsoft\Windows\CurrentVersion\Fonts')
except EnvironmentError:
raise FontNotFound('Can\'t open Windows font registry key')
try:
path = self._lookup_win(key, self.font_name, STYLES['NORMAL'], True)
self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
path = self._lookup_win(key, self.font_name, STYLES[style])
if path:
self.fonts[style] = ImageFont.truetype(path, self.font_size)
else:
if style == 'BOLDITALIC':
self.fonts[style] = self.fonts['BOLD']
else:
self.fonts[style] = self.fonts['NORMAL']
finally:
_winreg.CloseKey(key)
def _autodetect_official_installreg(self):
try:
key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\PUTTY_is1",
access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY)
value = winreg.QueryValueEx(key, "InstallLocation")[0]
winreg.CloseKey(key)
exe_file = os.path.join(value, self.EXE_NAME_OFFICIAL)
if os.path.exists(exe_file):
return exe_file
except:
pass
return None
def _autodetect_official_installreg(self):
try:
key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\FileZilla Client",
access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY)
value = winreg.QueryValueEx(key, "InstallLocation")[0]
winreg.CloseKey(key)
exe_file = os.path.join(value, self.EXE_NAME_OFFICIAL)
if os.path.exists(exe_file):
return exe_file
except:
pass
return None
def _autodetect_official_installreg(self):
try:
key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\winscp3_is1",
access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY)
value = winreg.QueryValueEx(key, "InstallLocation")[0]
winreg.CloseKey(key)
exe_file = os.path.join(value, self.EXE_NAME_OFFICIAL)
if os.path.exists(exe_file):
return exe_file
except:
pass
return None
def _getHiveAndKey(self, fullKey):
'''Split a windows registry key in two parts: the hive (hkey) and the registry key
Eg: "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion" will return: (_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion")
'''
str_hkey, str_key = fullKey.split('\\', 1)
hiveType = WinRegHiveType.getByName(str_hkey.lower())
return hiveType.hive, str_key
def exportRegKeysToString(self, key):
"""Exports Windows registry key to a string
This function exports a Windows registry key to a string (ini-file format).
@param key: The registry key to export. The key should include the section. Eg. "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion"
@type key: string
"""
strBuffer = StringIO()
regfile = IniFile(strBuffer)
self._addValuesRecursively(regfile, key)
return regfile.getContent()
def exportRegKeysToFile(self, key, path):
"""Exports Windows registry key to a file
This function exports a Windows registry key to an ini-file.
@param key: The registry key to export. The key should include the section. Eg. "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion"
@type key: string
@param path: The path of the file to export to
@type path: string
"""
j.sal.fs.writeFile(path, self.exportRegKeysToString(key))