python中拒绝访问

2024-04-25 20:41:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过以下代码使用python添加一个注册表项:

import _winreg
from time import sleep
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\\Microsoft\\Windows\\CurrentVersion\\Run',_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(key,'Windows-Update',0,_winreg.REG_BINARY,'C:\Windows\System32\SystemSetting\Block.exe') 
key.Close()

但它显示了这个错误WindowsError: [Error 5] Access is denied。在

有什么解决办法吗?在

编辑-我已经作为管理员运行它

EDIT2-它与KEY_ALL_ACCESS有关吗


Tags: key代码fromimporttime注册表windowssoftware
2条回答

这不是关于作为管理员运行。我尝试以管理员身份运行,但仍然收到Acces is denied消息。在

必须使用默认为0的保留整数。在

_winreg.OpenKey(key, sub_key[, res[, sam]]) ... res is a reserved integer, and must be zero. The default is zero.

所以,应该是这样的:

key = wreg.OpenKey(wreg.HKEY_LOCAL_MACHINE, "Software\\TestCompany\\TestProject",0, wreg.KEY_SET_VALUE)

实际上,您不必像建议的那样使用KEY_ALL_ACCESS,也不必像建议的那样使用here。只需在_winreg.KEY_SET_VALUE之前加上0。在

在命令提示符内运行python程序。windows中有一个command prompt (Admin)程序。或者只需右键单击Command prompt,然后选择Run as administratorRef

相关问题 更多 >