我们从Python开源项目中,提取了以下39个代码示例,用于说明如何使用winreg.HKEY_CURRENT_USER。
def _get_win_folder_from_registry(csidl_name): """This is a fallback technique at best. I'm not sure if using the registry for this guarantees us the correct answer for all CSIDL_* names. """ if PY3: import winreg as _winreg else: import _winreg shell_folder_name = { "CSIDL_APPDATA": "AppData", "CSIDL_COMMON_APPDATA": "Common AppData", "CSIDL_LOCAL_APPDATA": "Local AppData", }[csidl_name] key = _winreg.OpenKey( _winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" ) dir, type = _winreg.QueryValueEx(key, shell_folder_name) return dir
def test_reg_class(self): from distutils.msvc9compiler import Reg self.assertRaises(KeyError, Reg.get_value, 'xxx', 'xxx') # looking for values that should exist on all # windows registeries versions. path = r'Control Panel\Desktop' v = Reg.get_value(path, 'dragfullwindows') self.assertIn(v, ('0', '1', '2')) import winreg HKCU = winreg.HKEY_CURRENT_USER keys = Reg.read_keys(HKCU, 'xxxx') self.assertEqual(keys, None) keys = Reg.read_keys(HKCU, r'Control Panel') self.assertIn('Desktop', keys)
def _find_exe_in_registry(self): try: from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER except ImportError: from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER import shlex keys = ( r"SOFTWARE\Classes\FirefoxHTML\shell\open\command", r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command" ) command = "" for path in keys: try: key = OpenKey(HKEY_LOCAL_MACHINE, path) command = QueryValue(key, "") break except OSError: try: key = OpenKey(HKEY_CURRENT_USER, path) command = QueryValue(key, "") break except OSError: pass else: return "" if not command: return "" return shlex.split(command)[0]
def __init__(self, scope): self.scope = scope if scope == 'user': self.root = winreg.HKEY_CURRENT_USER self.subkey = 'Environment' else: self.root = winreg.HKEY_LOCAL_MACHINE self.subkey = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
def _find_exe_in_registry(self): try: from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER except ImportError: from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER import shlex keys = (r"SOFTWARE\Classes\FirefoxHTML\shell\open\command", r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command") command = "" for path in keys: try: key = OpenKey(HKEY_LOCAL_MACHINE, path) command = QueryValue(key, "") break except OSError: try: key = OpenKey(HKEY_CURRENT_USER, path) command = QueryValue(key, "") break except OSError: pass else: return "" if not command: return "" return shlex.split(command)[0]
def get_previous_install_prefixes(pyversion, arch, allusers=True): """Returns a list of prefixes for all old installations of this arch so that they can be removed from PATH if present. Note, it would be preferable to uninstall them properly instead. """ if allusers: # All Users key, subkey = (reg.HKEY_LOCAL_MACHINE, r'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\') else: # Just Me key, subkey = (reg.HKEY_CURRENT_USER, r'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\') keylist = [] # We ignore pyversion and instead look for any *conda installations. regex = re.compile('Python \S+ \(\S+conda[0-9]+ \S+ '+arch+'\)') _reg_query_sub_keys(key, subkey, keylist) results = [] for uninstsubkey in keylist: final_part = os.path.basename(uninstsubkey.rstrip('\\')) if regex.match(final_part): try: with reg.OpenKeyEx(key, uninstsubkey, 0, reg.KEY_QUERY_VALUE) as keyhandle: reg_value = reg.QueryValueEx(keyhandle, 'UninstallString') results.append(os.path.dirname(re.sub(r'^"|"$', '', reg_value[0]))) except: pass return results
def _get_nasm(self): p = self._winreg_read(winreg.HKEY_CURRENT_USER, r'SOFTWARE\nasm', '') return os.path.join(p[0], 'nasm.exe') if p is not None else None
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 findAtlasPath(): """Attempt to extract Atlas path from registry""" for v in vkeys: try: key = winreg.OpenKey(HKEY_CURRENT_USER, vkeys[v], 0, KEY_READ) val = winreg.QueryValueEx(key, r"TRENV EJ") atlasPath = val[0].rpartition('\\')[0] return atlasPath except: pass raise MissingAtlasException()
def checkstartup(): # check if it is linux if os_type == "Linux" or os_type == "FreeBSD" or os_type == 'OpenBSD': # check if the startup exists if os.path.exists(home_address + "/.config/autostart/persepolis.desktop"): return True else: return False # check if it is mac elif os_type == "Darwin": # OS X if os.path.exists(home_address + "/Library/LaunchAgents/com.persepolisdm.plist"): return True else: return False # check if it is Windows elif os_type == "Windows": # try to open startup key and check persepolis value try: aKey = winreg.OpenKey( winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, winreg.KEY_ALL_ACCESS) startupvalue = winreg.QueryValueEx(aKey, 'persepolis') startup = True except WindowsError: startup = False # Close the connection winreg.CloseKey(aKey) # if the startup enabled or disabled if startup: return True if not startup: return False # add startup file
def removestartup(): # check if it is linux if os_type == 'Linux' or os_type == 'FreeBSD' or os_type == 'OpenBSD': # remove it os.remove(home_address + "/.config/autostart/persepolis.desktop") # check if it is mac OS elif os_type == "Darwin": # OS X if checkstartup(): os.system('launchctl unload ' + home_address + "/Library/LaunchAgents/com.persepolisdm.plist") os.remove(home_address + "/Library/LaunchAgents/com.persepolisdm.plist") # check if it is Windows elif os_type == 'Windows': if checkstartup(): # Connect to the startup path in Registry key = winreg.OpenKey( winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, winreg.KEY_ALL_ACCESS) # remove persepolis from startup winreg.DeleteValue(key, 'persepolis') # Close connection winreg.CloseKey(key)
def remove_from_system_path(pathname, allusers=True, path_env_var='PATH'): """Removes all entries from the path which match the value in 'pathname' You must call broadcast_environment_settings_change() after you are finished manipulating the environment with this and other functions. For example, # Remove Anaconda from PATH remove_from_system_path(r'C:\Anaconda') broadcast_environment_settings_change() """ pathname = path.normcase(path.normpath(pathname)) envkeys = [(reg.HKEY_CURRENT_USER, r'Environment')] if allusers: envkeys.append((reg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment')) for root, keyname in envkeys: key = reg.OpenKey(root, keyname, 0, reg.KEY_QUERY_VALUE|reg.KEY_SET_VALUE) reg_value = None try: reg_value = reg.QueryValueEx(key, path_env_var) except WindowsError: # This will happen if we're a non-admin install and the user has # no PATH variable. reg.CloseKey(key) continue try: any_change = False results = [] for v in reg_value[0].split(os.pathsep): vexp = sz_expand(v, reg_value[1]) # Check if the expanded path matches the # requested path in a normalized way if path.normcase(path.normpath(vexp)) == pathname: any_change = True else: # Append the original unexpanded version to the results results.append(v) modified_path = os.pathsep.join(results) if any_change: reg.SetValueEx(key, path_env_var, 0, reg_value[1], modified_path) except: # If there's an error (e.g. when there is no PATH for the current # user), continue on to try the next root/keyname pair reg.CloseKey(key)
def jit(vdb, line): ''' Enable/Disable the current VDB location as the current Just-In-Time debugger for windows applications. Usage: jitenable [-D] -E Enable VDB JIT debugging -D Disable JIT debugging ''' argv = e_cli.splitargs(line) try: opts, args = getopt.getopt(argv, "ED") except Exception as e: return vdb.do_help('jit') try: import winreg except Exception as e: vdb.vprint('Error Importing _winreg: %s' % e) return HKLM = winreg.HKEY_LOCAL_MACHINE HKCU = winreg.HKEY_CURRENT_USER REG_SZ = winreg.REG_SZ regpath = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug' # wow64path = r'SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AeDebug' # regkey = _winreg.CreateKey(HKLM, regpath) regkey = winreg.CreateKey(HKLM, regpath) vdb.vprint('JIT Currently: %s' % winreg.QueryValueEx(regkey, 'Debugger')[0]) setval = None for opt, optarg in opts: if opt == '-D': setval = '' elif opt == '-E': vdbpath = os.path.abspath(sys.argv[0]) setval = '%s %s -r -p %%ld -e %%Id' % (sys.executable, vdbpath) # _winreg.SetValue(HKLM if setval != None: vdb.vprint('Setting JIT: %s' % (setval,)) winreg.SetValueEx(regkey, 'Debugger', None, REG_SZ, setval)
def _detect_distro_official(self, given_enabled, given_label, given_path): dist_props = { 'enabled': given_enabled, 'label': given_label, 'exe_file': None, 'cmd_args': ['-load', '%1'], 'sessions': []} # label if not dist_props['label']: dist_props['label'] = "PuTTY" # enabled? don't go further if not if dist_props['enabled'] is None: dist_props['enabled'] = True if not dist_props['enabled']: return dist_props # find executable exe_file = None if given_path: exe_file = os.path.normpath(os.path.join(given_path, self.EXE_NAME_OFFICIAL)) if not os.path.exists(exe_file): exe_file = None if not exe_file: exe_file = self._autodetect_official_installreg() if not exe_file: exe_file = self._autodetect_startmenu(self.EXE_NAME_OFFICIAL, "PuTTY.lnk") if not exe_file: exe_file = self._autodetect_official_progfiles() if not exe_file: exe_file = self._autodetect_path(self.EXE_NAME_OFFICIAL) #if not exe_file: # exe_file = self._autodetect_startmenu(self.EXE_NAME_OFFICIAL, "*putty*.lnk") if not exe_file: return None dist_props['exe_file'] = exe_file # list configured sessions try: hkey = winreg.OpenKey( winreg.HKEY_CURRENT_USER, 'Software\\SimonTatham\\PuTTY\\Sessions') index = 0 while True: try: dist_props['sessions'].append(urllib.parse.unquote( winreg.EnumKey(hkey, index), encoding='mbcs')) index += 1 except OSError: break winreg.CloseKey(hkey) except OSError: pass return dist_props