如下修改您的代码。如果是的话,它工作得很好Run as Administrator
。我在Win7 64位上测试过
import os, sys
import _winreg
import win32api
import win32security
#
# You need to have SeBackupPrivilege enabled for this to work
#
priv_flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
hToken = win32security.OpenProcessToken (win32api.GetCurrentProcess (), priv_flags)
privilege_id = win32security.LookupPrivilegeValue (None, "SeBackupPrivilege")
win32security.AdjustTokenPrivileges (hToken, 0, [(privilege_id, win32security.SE_PRIVILEGE_ENABLED)])
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows\CurrentVersion\Run')
filepath = r'C:\key.reg'
if os.path.exists (filepath):
os.unlink (filepath)
_winreg.SaveKey (key, filepath)
注意:如果win32api
&丢失,从这里win32security
安装它们
参考:这里