1

我想通过以下代码使用 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

4

2 回答 2

1

在命令提示符下运行 python 程序。windows中有一个command prompt (Admin)程序可用。或者直接右键单击Command prompt并选择Run as administrator参考

于 2016-04-09T13:00:32.480 回答
1

这与以管理员身份运行无关。我尝试以管理员身份运行,但仍然收到Acces is denied消息。

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

_winreg.OpenKey(key, sub_key[, res[, sam]]) ... res 是保留整数,必须为零。默认为零。

所以,它应该是这样的:

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

您实际上不必KEY_ALL_ACCESS这里建议的那样使用。只需在0之前添加_winreg.KEY_SET_VALUE

于 2017-06-29T05:52:42.817 回答