使用python37编写脚本读取win10系统中U盘历史使用记录并用图形界面展示
要读取win10系统中U盘的历史使用记录,可以使用Python的winreg模块来访问Windows注册表。以下是一个使用Python 3.7编写的脚本,可以读取U盘的历史使用记录,并使用图形界面展示:
import tkinter as tk
import winreg
def get_usb_history():
usb_history = []
# 打开U盘历史使用记录的注册表键
key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\KnownDevices"
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path)
except FileNotFoundError:
return usb_history
# 逐个读取子键,并获取U盘名称和最后访问时间
index = 0
while True:
try:
sub_key_name = winreg.EnumKey(key, index)
sub_key = winreg.OpenKey(key, sub_key_name)
label = winreg.QueryValueEx(sub_key, "Label")[0]
last_access_time = winreg.QueryInfoKey(sub_key)[2]
usb_history.append((label, last_access_time))
winreg.CloseKey(sub_key)
index += 1
except OSError:
break
winreg.CloseKey(key)
return usb_history
def show_usb_history():
usb_history = get_usb_history()
# 创建主窗口
root = tk.Tk()
root.title("U盘历史使用记录")
# 创建列表框
listbox = tk.Listbox(root)
listbox.pack(fill=tk.BOTH, expand=True)
# 在列表框中显示U盘历史使用记录
for usb in usb_history:
label, last_access_time = usb
listbox.insert(tk.END, f"U盘名称:{label},最后访问时间:{last_access_time}")
# 运行主循环
root.mainloop()
# 运行展示U盘历史使用记录的函数
show_usb_history()
在运行脚本后,将会弹出一个图形界面窗口,显示U盘的历史使用记录。每条记录包含U盘的名称和最后访问时间。
请注意,脚本中使用的注册表路径SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\KnownDevices是Windows 10系统中U盘历史使用记录的默认路径。如果在不同的系统中路径有所不同,需要相应地修改脚本
原文地址: http://www.cveoy.top/t/topic/hYpl 著作权归作者所有。请勿转载和采集!