def delete_reg(): reg_root = win32con.HKEY_CLASSES_ROOT # 键的路径(具体路径自行修改) reg_path = "Directory\\Background\\shell\\wallpaper\\command" # 权限和参数设置 reg_flags = win32con.WRITE_OWNER | win32con.KEY_WOW64_64KEY | win32con.KEY_ALL_ACCESS try: # 判断注册表中键值是否存在 key = win32api.RegOpenKeyEx(reg_root, reg_path, 0, reg_flags) except Exception as e: print(e) key = None key_flag = False if key is None: key_flag = False else: key_flag = True win32api.RegCloseKey(key) if key_flag: try: # 删除值(key也有close方法,可以用with结构) # with win32api.RegOpenKeyEx(reg_root, reg_path, 0, reg_flags) as key: # win32api.RegDeleteValue(key, 'test_value') # 删除键(需要获取其父键,通过父键删除子键) reg_parent, subkey_name = os.path.split(reg_path) try: key2 = win32api.RegOpenKeyEx(reg_root, reg_parent, 0, reg_flags) except Exception as e: print(e) key2 = None key_flag2 = False if key2 is None: key_flag2 = False else: key_flag2 = True if key_flag2: win32api.RegDeleteKeyEx(key2, subkey_name) win32api.RegCloseKey(key2) reg_parent2, subkey_name2 = os.path.split(reg_parent) try: key3 = win32api.RegOpenKeyEx(reg_root, reg_parent2, 0, reg_flags) except Exception as e: print(e) key3 = None key_flag3 = False if key3 is None: key_flag3 = False else: key_flag3 = True if key_flag3: win32api.RegDeleteKeyEx(key3, subkey_name2) win32api.RegCloseKey(key3) else: win32api.RegCloseKey(key3) pass else: pass except Exception as e: print(e) sys.exit()
def del_key(self, path): key = win32api.RegOpenKeyEx(self.root, path, 0, self.flags) m_item = win32api.RegEnumKeyEx(key) if not m_item: reg_parent, subkey_name = os.path.split( path) # 获得父路径名字 和自己的名字,而不是路径 try: key_parent = win32api.RegOpenKeyEx(self.root, reg_parent, 0, self.flags) # 看这个节点是否可被访问 win32api.RegDeleteKeyEx(key_parent, subkey_name) # 删除这个节点 return except Exception as e: print("Bently 被拒绝访问") return for item in win32api.RegEnumKeyEx(key): # 递归加子节点 strRecord = item[0] # 采用key的第一个节点,item里面是元组,获取第一个名字。就是要的子项名字 newpath = path + '\\' + strRecord self.del_key(newpath) # 删除父节点 root_parent, child_name = os.path.split(path) try: # 看这个节点是否可被访问 current_parent = win32api.RegOpenKeyEx(self.root, root_parent, 0, self.flags) win32api.RegDeleteKeyEx(current_parent, child_name) except Exception as e: print("Bently 被拒绝访问") return
def delete_sub_key(self, sub_key_name): """delete sub key""" key = self.get_key() win32api.RegDeleteKeyEx(key, sub_key_name) key.close() return True
def delete_current_key(self): """delete current key""" parent, key_name = os.path.split(self.path) key_parent = win32api.RegOpenKeyEx(self.root, parent, 0, REG_FLAGS) win32api.RegDeleteKeyEx(key_parent, key_name) key_parent.close() return True
## reopen key in transacted mode transacted_key=win32api.RegOpenKeyTransacted(Key=win32con.HKEY_CURRENT_USER, SubKey=keyname, Transaction=trans, samDesired=win32con.KEY_ALL_ACCESS) subkey, disp=win32api.RegCreateKeyEx(transacted_key, subkeyname, Transaction=trans, samDesired=win32con.KEY_ALL_ACCESS, Class=classname) ## Newly created key should not be visible from non-transacted handle subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)] assert subkeyname not in subkeys transacted_subkeys=[s[0] for s in win32api.RegEnumKeyExW(transacted_key)] assert subkeyname in transacted_subkeys ## Key should be visible to non-transacted handle after commit win32transaction.CommitTransaction(trans) subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)] assert subkeyname in subkeys ## test transacted delete del_trans=win32transaction.CreateTransaction(Description='test RegDeleteKeyTransacted') win32api.RegDeleteKeyEx(key, subkeyname, Transaction=del_trans) ## subkey should still show up for non-transacted handle subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)] assert subkeyname in subkeys ## ... and should be gone after commit win32transaction.CommitTransaction(del_trans) subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)] assert subkeyname not in subkeys win32api.RegDeleteKey(win32con.HKEY_CURRENT_USER, keyname)