我们从Python开源项目中,提取了以下36个代码示例,用于说明如何使用_winreg.DeleteKey()。
def unregister(classobj): import _winreg subKeyCLSID = "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s" % classobj._reg_clsid_ try: hKey = _winreg.CreateKey( _winreg.HKEY_LOCAL_MACHINE, subKeyCLSID ) subKey = _winreg.DeleteValue( hKey, "ButtonText" ) _winreg.DeleteValue( hKey, "ClsidExtension" ) # for calling COM object _winreg.DeleteValue( hKey, "CLSID" ) _winreg.DeleteValue( hKey, "Default Visible" ) _winreg.DeleteValue( hKey, "ToolTip" ) _winreg.DeleteValue( hKey, "Icon" ) _winreg.DeleteValue( hKey, "HotIcon" ) _winreg.DeleteKey( _winreg.HKEY_LOCAL_MACHINE, subKeyCLSID ) except WindowsError: print "Couldn't delete Standard toolbar regkey." else: print "Deleted Standard toolbar regkey." # # test implementation #
def rdelete(self, key, subkey): """ Recursively delete a Windows registry key. This function will remove a key, even if it has child keys. There is no equivalent in _winreg. """ if type(key) is str: hkey = Registry.map_key(key) elif type(key) is Registry.RegistryKey: hkey = key.getkey() else: hkey = key if type(subkey) is str: subkey = self.open(hkey, subkey) # test2 childkeys = subkey.keys() for keyname in childkeys: # print 'Child=>',keyname childkey = self.open(subkey.getkey(), keyname) self.rdelete(subkey, childkey) # print subkey.getkeyname() wreg.DeleteKey(hkey, subkey.getkeyname())
def DllUnregisterServer(): import _winreg paths = [ "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Desktop\\Namespace\\" + ShellFolder._reg_clsid_, "%s\\shellex\\ContextMenuHandlers\\%s" % (ContextMenu._context_menu_type_, ContextMenu._reg_desc_), ] for path in paths: try: _winreg.DeleteKey(_winreg.HKEY_LOCAL_MACHINE, path) except WindowsError, details: import errno if details.errno != errno.ENOENT: print "FAILED to remove %s: %s" % (path, details) propsys.PSUnregisterPropertySchema(get_schema_fname()) print ShellFolder._reg_desc_, "unregistration complete."
def DllUnregisterServer(): import _winreg try: key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, "directory\\shellex\\CopyHookHandlers\\" + ShellExtension._reg_desc_) except WindowsError, details: import errno if details.errno != errno.ENOENT: raise try: key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, "*\\shellex\\CopyHookHandlers\\" + ShellExtension._reg_desc_) except WindowsError, details: import errno if details.errno != errno.ENOENT: raise print ShellExtension._reg_desc_, "unregistration complete."
def UnregisterAddin(klass): import _winreg try: _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Excel\\Addins\\" + klass._reg_progid_) except WindowsError: pass
def remove_jupyter_here(): for env in ('qtconsole', 'notebook'): try: winreg.DeleteKey( winreg.HKEY_CLASSES_ROOT, r'Directory\shell\jupyter_%s_here\Command' % env) winreg.DeleteKey( winreg.HKEY_CLASSES_ROOT, r'Directory\shell\jupyter_%s_here' % env) winreg.DeleteKey( winreg.HKEY_CLASSES_ROOT, r'Directory\Background\shell\jupyter_%s_here\Command' % env) winreg.DeleteKey( winreg.HKEY_CLASSES_ROOT, r'Directory\Background\shell\jupyter_%s_here' % env) print("Jupyter %s here context menu entry removed." % env) except: # If this fails it is because it was not installed, so nothing to # worry about. pass
def _delete_key_if_empty(self, service): key_name = r'Software\%s\Keyring' % service key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_name, 0, winreg.KEY_ALL_ACCESS) try: winreg.EnumValue(key, 0) return except WindowsError: pass winreg.CloseKey(key) # it's empty; delete everything while key_name != 'Software': parent, sep, base = key_name.rpartition('\\') key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, parent, 0, winreg.KEY_ALL_ACCESS) winreg.DeleteKey(key, base) winreg.CloseKey(key) key_name = parent
def __del_keys(self): 'Private class method.' try: while True: _winreg.DeleteKey(self.__self, _winreg.EnumKey(self.__self, 0)) except EnvironmentError: pass
def __delitem__(self, key): 'Delete the specified key.' _winreg.DeleteKey(self.__self, key)
def __del_keys(self): 'Private class method.' try: while True: _winreg.DeleteKey(self.__key, _winreg.EnumKey(self.__key, 0)) except EnvironmentError: pass
def __delitem__(self, key): 'Delete the specified key.' _winreg.DeleteKey(self.__key, key)
def delete(self, key, subkey): """ Deletes a windows registry key. Same as DeleteKey of _winreg """ if type(key) is str: hkey = self.map_key(key) else: hkey = key if not hkey: raise RegistryError,"could not find registry key for %s" % key try: wreg.DeleteKey(hkey, subkey) except EnvironmentError, e: raise RegistryError, e
def DllUnregisterServer(): import _winreg kn = r"Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\%s" \ % (EmptyVolumeCache._reg_desc_,) try: key = _winreg.DeleteKey(_winreg.HKEY_LOCAL_MACHINE, kn) except WindowsError, details: import errno if details.errno != errno.ENOENT: raise print EmptyVolumeCache._reg_desc_, "unregistration complete."
def DllUnregisterServer(): import _winreg try: key = _winreg.DeleteKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\" \ "Explorer\\Desktop\\Namespace\\" + \ ShellFolderRoot._reg_clsid_) except WindowsError, details: import errno if details.errno != errno.ENOENT: raise print ShellFolderRoot._reg_desc_, "unregistration complete."
def DllUnregisterServer(): import _winreg try: key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, "Python.File\\shellex\\IconHandler") except WindowsError, details: import errno if details.errno != errno.ENOENT: raise print ShellExtension._reg_desc_, "unregistration complete."
def DllUnregisterServer(): import _winreg try: key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT, "Python.File\\shellex\\ContextMenuHandlers\\PythonSample") except WindowsError, details: import errno if details.errno != errno.ENOENT: raise print ShellExtension._reg_desc_, "unregistration complete."
def UnregisterAddin(klass): import _winreg try: _winreg.DeleteKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Office\\Outlook\\Addins\\" + klass._reg_progid_) except WindowsError: pass
def restore(self, key, value=''): """ Restore to the last registry known state """ if self.no_restore is False: new_sk = self.last_created['new_sk'] k = self.last_created['key'] exist_sk = self.last_created['existing_sk'] self.del_value(key, value) if new_sk is not None: for i in range(len(new_sk)): if i == 0: try: winreg.DeleteKey(k, "\\".join(exist_sk + new_sk)) except WindowsError as error: pass else: try: winreg.DeleteKey(k, "\\".join( exist_sk + new_sk[:-i])) except WindowsError as error: pass self.last_created['new_sk'] = None self.last_created['existing_sk'] = None self.last_created['key'] = None
def delete_key(self, key, subkey): """ Deletes a particular key """ try: return winreg.DeleteKey(key, subkey) except WindowsError as error: print "Error al eliminar la clave"
def remove_certificate(self, certificate): CONTAINS_SUBKEYS = 0 registryHandles = [] returnValue = False path = certificate["RegPath"].split("/") hiveName = path.pop(0) keyName = path.pop(-1) hive = reg.ConnectRegistry(None, self.regKeys[hiveName][0]) registryHandle = reg.OpenKey(hive, self.regKeys[hiveName][1]) self._log("Connected to registry at location: %s" % hiveName) for step in path: registryHandles.append(registryHandle) registryHandle = reg.OpenKey(registryHandle, step) try: deletionCandidate = reg.OpenKey(registryHandle, keyName) self._log("Querying deletion canditate: %s" % certificate["RegPath"]) if not reg.QueryInfoKey(deletionCandidate)[CONTAINS_SUBKEYS]: self._log("Attempting to delete key: %s" % certificate["RegPath"]) reg.CloseKey(deletionCandidate) reg.DeleteKey(registryHandle, keyName) self._log("Deleted key: %s" % certificate["RegPath"]) returnValue = True else: self._error_log("Unable to delete key: %s. Key contains subkeys." % certificate["RegPath"]) registryHandles.append(deletionCandidate) raise WindowsError except WindowsError as e: self._error_log("Unable to delete key: %s. Windows error." % certificate["RegPath"]) self._error_log("%s: %s" % (certificate["RegPath"], str(e))) pass self._log("Closing registry handles...") for handle in registryHandles: reg.CloseKey(handle) reg.CloseKey(hive) self._log("Registry handles closed.") return returnValue
def del_subkey(self,name): """Delete the named subkey, and any values or keys it contains.""" self.sam |= KEY_WRITE subkey = self.get_subkey(name) subkey.clear() _winreg.DeleteKey(subkey.parent.hkey,subkey.name)
def __delitem__(self, key): # Delete a string value or a subkey, depending on the type try: item = self[key] except: return # Silently ignore bad keys itemtype = type(item) if itemtype is str: _winreg.DeleteValue(self.keyhandle, key) elif isinstance(item, RegistryDict): # Delete everything in the subkey, then the subkey itself item.clear() _winreg.DeleteKey(self.keyhandle, key) else: raise ValueError, "Unknown item type in RegistryDict"
def RegisterPythonwin(register=True): """ Add (or remove) Pythonwin to context menu for python scripts. ??? Should probably also add Edit command for pys files also. Also need to remove these keys on uninstall, but there's no function like file_created to add registry entries to uninstall log ??? """ import os, distutils.sysconfig lib_dir = distutils.sysconfig.get_python_lib(plat_specific=1) classes_root=get_root_hkey() ## Installer executable doesn't seem to pass anything to postinstall script indicating if it's a debug build, pythonwin_exe = os.path.join(lib_dir, "Pythonwin", "Pythonwin.exe") pythonwin_edit_command=pythonwin_exe + ' /edit "%1"' keys_vals = [ ('Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Pythonwin.exe', '', pythonwin_exe), ('Software\\Classes\\Python.File\\shell\\Edit with Pythonwin', 'command', pythonwin_edit_command), ('Software\\Classes\\Python.NoConFile\\shell\\Edit with Pythonwin', 'command', pythonwin_edit_command), ] try: if register: for key, sub_key, val in keys_vals: ## Since winreg only uses the character Api functions, this can fail if Python ## is installed to a path containing non-ascii characters hkey = winreg.CreateKey(classes_root, key) if sub_key: hkey = winreg.CreateKey(hkey, sub_key) winreg.SetValueEx(hkey, None, 0, winreg.REG_SZ, val) hkey.Close() else: for key, sub_key, val in keys_vals: try: winreg.DeleteKey(classes_root, key) except OSError, why: winerror = getattr(why, 'winerror', why.errno) if winerror != 2: # file not found raise finally: # tell windows about the change from win32com.shell import shell, shellcon shell.SHChangeNotify(shellcon.SHCNE_ASSOCCHANGED, shellcon.SHCNF_IDLIST, None, None)