1.首先定义了两个重要的常量,它们分别指定了 Windows 注册表中的两个关键路径。这段代码是用来设置和操作 Windows 注册表的参数。下面逐一解释这些行代码的含义:
HKEY_CURRENT_USER = winreg.HKEY_CURRENT_USER
这行代码创建了一个变量 `HKEY_CURRENT_USER`,并将其设置为 `winreg` 模块中的 `HKEY_CURRENT_USER`。这表示这个变量指向当前登录用户的注册表项的顶级节点。在 Windows 注册表中,每个登录用户都有自己的 `HKEY_CURRENT_USER` 分支,里面包含了用户的个人设置。
PREMIUM_PATH = r'Software\PremiumSoft'
这行代码定义了一个字符串常量 `PREMIUM_PATH`,用来指定注册表中 PremiumSoft 应用程序相关配置的路径。
CLSID_PATH = r'Software\Classes\CLSID'
这行代码定义了另一个字符串常量 `CLSID_PATH`,它指向注册表中含有 COM类标识符(CLSID)的项的路径。`CLSID` 用于标识 COM 对象。同样,这个路径也是相对于 `HKEY_CURRENT_USER` 的,所以实际对应的完整路径是 `HKEY_CURRENT_USER\Software\Classes\CLSID`。一般来说,每个 COM 组件都有一个唯一的 CLSID 用于在系统内进行标识。
2.定义了一个 Python 函数 get_sub_keys
,它的目的是检索并返回指定注册表路径下的所有子键名称的列表。这个函数有两个参数:
root
:这是根注册表项的句柄。根据你的上下文,这可能是像HKEY_CURRENT_USER
或任何其他有效的顶级注册表项。reg_path
:这是一个字符串,代表从根注册表项下要枚举子键的特定路径。
def get_sub_keys(root: Any, reg_path: str) -> list:
"""This function will retrieve a list of sub-keys under the path
of `root` + `reg_path`.
Args:
root(Any): Root registry.
reg_path(str): The relative specific path under the root registry.
Returns:
The list of sub-keys.
"""
key_result = winreg.OpenKeyEx(root, reg_path)
i: int = 0
sub_keys_list: list = list()
while True:
try:
sub_keys = winreg.EnumKey(key_result, i)
sub_keys_list.append(sub_keys)
i += 1
except Exception as e:
break
return sub_keys_list
函数返回一个包含注册表子键名称列表的列表。如果在枚举过程中遇到任何异常,将会捕获异常,结束循环,并返回收集到的子键列表到目前为止。由于它使用了 while True:
,所以是一个无限循环,只有当 winreg.EnumKey
函数抛出异常时才会退出,通常情况下,当枚举完所有子键后,该函数会抛出一个异常。
3.定义了一个 Python 函数 get_all_keys
函数目的是遍历并收集指定注册表根路径下所有子项(包括嵌套子项)的绝对路径列表。该过程类似于进行宽度优先搜索(BFS),使用队列来管理待检查的子键。该函数接收两个参数:
root
:注册表的根部分的句柄。key_path
:相对于根部分的特定路径。
def get_all_keys(root: Any, key_path: str) -> list:
"""Get the list of absolute path of all entries under the
specified path through the deque.
Args:
root(Any): Root registry.
key_path(str): The relative specific path under the root registry.
Returns:
A list of all entries under the keys.
"""
all_keys_list: list = list()
qeque = deque()
qeque.append(key_path)
while len(qeque) != 0:
sub_key_path = qeque.popleft()
for item in get_sub_keys(root, sub_key_path):
item_path = os.path.join(sub_key_path, item)
if len(get_sub_keys(root, item_path)) != 0:
qeque.append(item_path)
all_keys_list.append(item_path)
else:
all_keys_list.append(item_path)
return all_keys_list
get_all_keys
的函数定义,其目的是遍历 Windows 注册表中的一个指定的根键 (root
) 和一个子键路径 (key_path
), 来获取所有子键的绝对路径列表。它依赖于前面定义的函数 get_sub_keys
来获取给定路径下的所有直接子键。
代码中的实现,该函数能够找到注册表中特定路径下的所有子项(包括多级子项)的绝对路径,这可以用于注册表分析或管理任务。
3.使用main
函数它是 Python 脚本中的主入口,用于执行一系列对 Windows 注册表的操作。具体来说,它涉及检索并可能删除当前用户的特定注册表键。以下是对 main
函数的代码:
def main():
"""The entry function to be executed.
Returns:
None
"""
clsid_all_keys_list = get_all_keys(HKEY_CURRENT_USER, CLSID_PATH)
premium_all_keys_list = get_all_keys(HKEY_CURRENT_USER, PREMIUM_PATH)
premium_sub_keys_list = [os.path.join(PREMIUM_PATH, item) for item in get_sub_keys(HKEY_CURRENT_USER, PREMIUM_PATH)]
print(f"premium_sub_keys_list: {premium_sub_keys_list}")
for clsid_item in clsid_all_keys_list:
if "Info" in clsid_item:
clsid_item_prefix = os.path.dirname(clsid_item)
print(f"# Info item: {clsid_item}")
winreg.DeleteKeyEx(HKEY_CURRENT_USER, clsid_item)
winreg.DeleteKeyEx(HKEY_CURRENT_USER, clsid_item_prefix)
# The outermost folder is not deleted.
for premium_item in reversed(premium_all_keys_list):
if "Servers" in premium_item:
print(f"Tips: Servers => {premium_item} will not be deleted.")
pass
elif premium_item in premium_sub_keys_list:
print(f"Tips: Servers => {premium_item} will not be deleted.")
pass
else:
winreg.DeleteKeyEx(HKEY_CURRENT_USER, premium_item)
if __name__ == "__main__":
print("Start to delete registry...")
main()
print("Task done.", "Windows will closed after 5 seconds...", sep="\n")
for i in range(5):
time.sleep(1)
print("*" * (i + 1))