读取注册表的Python代码

读取注册表的Python代码

本文介绍了读取注册表的Python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from _winreg import *

"""print r"*** Reading from SOFTWARE\Microsoft\Windows\CurrentVersion\Run ***" """
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)

aKey = OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")
for i in range(1024):
    try:
        asubkey=EnumKey(aKey,i)
        val=QueryValueEx(asubkey, "DisplayName")
        print val
    except EnvironmentError:
        break

谁能更正错误...我只想在 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 键的子项中显示DisplayName"这是我得到的错误..

Could anyone please correct the error...i just want to display the "DisplayName" within the subkeys of the key the HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\UninstallThis is the error i get..

Traceback (most recent call last):
  File "C:/Python25/ReadRegistry", line 10, in <module>
    val=QueryValueEx(asubkey, "DisplayName")
TypeError: The object is not a PyHKEY object

推荐答案

文档 表示 EnumKey 返回带有键名的字符串.您必须使用 _winreg.OpenKey 函数显式打开它.我已经修复了您的代码片段:

Documentation says that EnumKey returns string with key's name. You have to explicitly open it with _winreg.OpenKey function. I've fixed your code snippet:

from _winreg import *

aKey = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
aReg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)

print(r"*** Reading from %s ***" % aKey)

aKey = OpenKey(aReg, aKey)
for i in range(1024):
    try:
        asubkey_name = EnumKey(aKey, i)
        asubkey = OpenKey(aKey, asubkey_name)
        val = QueryValueEx(asubkey, "DisplayName")
        print(val)
    except EnvironmentError:
        break

请注意,并非每个键都有DisplayName";可用价值.

Please note, that not every key has "DisplayName" value available.

这篇关于读取注册表的Python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:13