gpt4 book ai didi

python - winreg.OpenKey 为现有注册表项抛出 filenotfound 错误

转载 作者:太空狗 更新时间:2023-10-29 20:10:47 25 4
gpt4 key购买 nike

我在读取由我的软件创建的注册表项时遇到困难。但是,使用相同的代码,我能够读取其他键。

installdir = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\Types"
) #this works perfect
#installdir1 = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\\MySoftware\\MyEvent\\IS"
) #this gives Filenotfound error

# list values owned by this registry key
try:
i = 0
while 1:
name, value, type = winreg.EnumValue(installdir, i)
print (repr(name))
i += 1
except WindowsError:
print ("Bot donf")
value, type = winreg.QueryValueEx(installdir, "10")
print("user is", repr(value))

value, type = winreg.QueryValueEx(winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS"), "v2")
print("user is", repr(value))

回溯显示

 Traceback (most recent call last):
File "D:/python_scripts/myclass.py", line 32, in <module>
value, type = winreg.QueryValueEx(winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS"), "v2")
FileNotFoundError: [WinError 2] The system cannot find the file specified

但是 Windows reg 查询能够检索值集。

#reg query HKLM\SOFTWARE\MySoftware\MyEvent\IS /v v2

HKEY_LOCAL_MACHINE\SOFTWARE\MySoftware\MyEvent\IS
v2 REG_DWORD 0x12

任何帮助将不胜感激

最佳答案

注册表有 2 个 View 。有 32 位注册 TableView 和 64 位注册 TableView 。默认情况下,在大多数情况下,32 位应用程序只能看到 32 位注册 TableView ,64 位应用程序只能看到 64 位注册 TableView 。

可以使用 KEY_WOW64_64KEY 或 KEY_WOW64_32KEY 访问标志访问另一个 View 。

如果您正在运行 32 位 python 并且您的 key 是 64 位注册 TableView 的一部分,您应该使用类似这样的东西来打开您的 key :

winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS", access=winreg.KEY_READ | winreg.KEY_WOW64_64KEY)

如果您正在运行 64 位 python 并且您的 key 是 32 位注册 TableView 的一部分,您应该使用类似这样的东西来打开您的 key :

winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS", access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY)

如果您知道 key 始终是同一 View 的一部分,添加适当的 KEY_WOW64_* 访问标志将确保无论您的 python 架构如何,它都能正常工作。

在最一般的情况下,如果您有可变的 Python 体系结构并且您事先不知道 key 将在哪个 View 中,您可以尝试在当前 View 中找到 key ,然后尝试另一个 View 。它可能看起来像这样:

try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS")
except FileNotFoundError:
import platform

bitness = platform.architecture()[0]
if bitness == '32bit':
other_view_flag = winreg.KEY_WOW64_64KEY
elif bitness == '64bit':
other_view_flag = winreg.KEY_WOW64_32KEY

try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS", access=winreg.KEY_READ | other_view_flag)
except FileNotFoundError:
'''
We really could not find the key in both views.
'''

有关更多信息,请查看 Accessing an Alternate Registry View .

关于python - winreg.OpenKey 为现有注册表项抛出 filenotfound 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com