注册表相关接口:
# 打开注册表
runpath = "Software\Microsoft\Windows\CurrentVersion\Run"
hKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, runpath, 0, win32con.KEY_SET_VALUE)
#添加删除键值
win32api.RegCreateKey(key, subKey )
win32api.RegDeleteKey (key, subKey )
#设置项值
win32api.RegSetValueEx(hKey, "MyTool", 0, win32con.REG_SZ, path)
#删除项值
win32api.RegDeleteValue(hKey, "MyTool")
# 关闭注册表
win32api.RegCloseKey(hKey)
实例:
import win32con
import win32api
import os,sys
def addfile2autorun(path,run):
"注册到启动项"
runpath = "Software\Microsoft\Windows\CurrentVersion\Run"
hKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, runpath, 0, win32con.KEY_ALL_ACCESS)
if run:
win32api.RegSetValueEx(hKey, "MyTool", 0, win32con.REG_SZ, path)
print(win32api.RegQueryValueEx(hKey,"MyTool"))
else:
win32api.RegDeleteValue(hKey, "MyTool")
win32api.RegCloseKey(hKey)
if __name__ == '__main__':
path = os.getcwd()+"\\"+sys.argv[0]
addfile2autorun(path,False)