Python刪除註冊表鍵值一例
有時出於某種需要,希望用Python對註冊表進行查找,創建,刪除等操作。
本例演示瞭如何用Python刪除註冊表鍵值,並用另一個小程序演示如何註冊該程序到註冊表中,使其每次開機都會執行:
RemoveValue.py: 刪除指定鍵值
#需安裝pywin32
import win32api
import win32con
def removeValue(name,value):
key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,name,0,win32con.KEY_ALL_ACCESS)
try:
win32api.RegDeleteValue(key,value)
except:
pass
name = 'SOFTWARE\\Scooter Software\\Beyond Compare 4'
value = 'CacheID'
removeValue(name,value)
addfile2autorun,添加RemoveValue到runpath,運行一次即可。
import win32con
import win32api
import os
def addfile2autorun(path):
"註冊到啓動項"
runpath = "Software\Microsoft\Windows\CurrentVersion\Run"
hKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, runpath, 0, win32con.KEY_SET_VALUE)
(filepath, filename) = os.path.split(path)
win32api.RegSetValueEx(hKey, "RemoveValue", 0, win32con.REG_SZ, path)
win32api.RegCloseKey(hKey)
addfile2autorun('C:\Program Files\Beyond Compare 4\RemoveValue.py')