所以我得到了标题中命名的错误。对不起,如果我很天真,但我是本地 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)