Python Winreg - WindowsError:[错误5]访问被拒绝

时间:2016-09-24 09:18:05

标签: python python-2.7 registry winreg

所以我收到了标题中指定的错误。对不起,如果我是天真的,但我是一个原生的OSX用户,而且不经常为Windows写作。在下面的代码中,我在条件语句中打开密钥时授予了winreg.KEY_ALL_ACCESS

请参阅下面的代码。如果您认为代码更简洁,请给我建议。代码只是确定哪个子项具有特定的条目/值,然后将条目/值写入该子项。

import _winreg as winreg
import socket

with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces') as key:
    keys = [winreg.EnumKey(key, i) for i in range(winreg.QueryInfoKey(key)[0])]
    for subkey in keys:
        with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\{0}'.format(subkey)) as finalkey:
            values = [winreg.EnumValue(finalkey, i) for i in range(winreg.QueryInfoKey(finalkey)[1])]
            for value in values:
                if value[0] == "DhcpIPAddress" and value[1] == socket.gethostbyname(socket.gethostname()):
                    with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\{0}'.format(subkey), 0, winreg.KEY_ALL_ACCESS) as writetokey:
                        winreg.SetValueEx(writetokey, 'TcpAckFrequency', 0, winreg.REG_DWORD, 1)

0 个答案:

没有答案