windows 循环遍历值或注册表项.. _winreg Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3974038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:26:38  来源:igfitidea点击:

Loop through values or registry key.. _winreg Python

pythonwindowsregistryenumerationwinreg

提问by Zac Brown

How would I loop through allthe valuesof a Windows Registry Key using the Python module _winreg. I have code that will do what I want, but it is for the subkeys of the specified registry key.

如何将通过我环路所有使用Python模块_winreg一个Windows注册表项。我有代码可以做我想做的事,但它是针对指定注册表项的子项的。



Here Is The Code:

这是代码:

from _winreg import *
t = OpenKey(HKEY_CURRENT_USER, r"PATH TO KEY", 0, KEY_ALL_ACCESS)

try:
    i = 0
    while True:
        subkey = EnumValue(t, i)
        print subkey
        i += 1
except WindowsError:
    # WindowsError: [Errno 259] No more data is available    
    pass


Oh, figured it out. But, if anyone knows of another way to do it, I'll still accept that answer!

哦,想通了。但是,如果有人知道另一种方法来做到这一点,我仍然会接受这个答案!

采纳答案by pyfunc

Shouldn't EnumValue be of help here

EnumValue 不应该在这里有帮助

# list all values for a key
try:
    count = 0
    while 1:
        name, value, type = _winreg.EnumValue(t, count)
        print repr(name),
        count = count + 1
except WindowsError:
    pass

回答by VertigoRay

I prefer to avoidthe error instead of diving right into it ...

我宁愿避免错误,而不是一头扎进去......

Use _winreg.QueryInfoKeyto get the number of values:

使用_winreg.QueryInfoKey获取值的数量:

import _winreg
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r'PATH\TO\KEY', 0, _winreg.KEY_READ)

for i in xrange(0, _winreg.QueryInfoKey(key)[1]):
    print _winreg.EnumValue(key, i)

To get the number of Keys, same method, different index (second half of original question):

要获取键的数量,方法相同,索引不同(原始问题的后半部分):

for i in xrange(0, _winreg.QueryInfoKey(key)[0]):
    print _winreg.EnumKey(key, i)

Note:use importinstead of from ... importto make it explicit where functions and variables are coming from. Makes it easier to follow code later in life.

注意:使用import代替from ... import来明确函数和变量的来源。更容易在以后的生活中遵循代码。

回答by Anagnostou John

for python 3

对于蟒蛇 3

import winreg
hKey = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, "Local Settings\Software\Microsoft\Windows\Shell\MuiCache")


try:
    count = 0
    while 1:
        name, value, type = winreg.EnumValue(hKey, count)
        print (name),
        count = count + 1
except WindowsError as err:
    print(err)
    pass

回答by user12476651

(Python3) Using generators and recursivity as i don't like counters...

(Python3) 使用生成器和递归,因为我不喜欢计数器......

 def get_keys(self, path, i=0):
    try:
        yield winreg.EnumValue(winreg.OpenKey(winreg.HKEY_CURRENT_USER, path), i)
        yield from get_keys(path, i+1)
    except WindowsError as err:
        pass


for name, value, type in r.get_keys(r"Local Settings\Software\Microsoft\Windows\Shell\MuiCache"):
    print(f"{name} => {value} ({type})"

回答by Chris Calverley

it seems like this only works on certain key values, for instance i try to run on HKLM:Software\labtech\service there are like 70 different keys and values here, when i run this i only get 3 returns

似乎这只适用于某些键值,例如我尝试在 HKLM:Software\labtech\service 上运行,这里有 70 个不同的键和值,当我运行它时,我只得到 3 个返回

回答by Pabitra Pati

For iterating through keys and values of registry, you would need EnumKey()and EnumValue()method from _winregmodule. Note that these two methods, take index as an argument, and will provide you the key (or value) only for the given index. Therefore, in order to get all the keys (or values) you need to increment the index by one and continue until WindowsErrorhas not encountered.

对于通过按键和注册表的值迭代,你需要EnumKey()EnumValue()来自法_winreg模块。请注意,这两种方法都将索引作为参数,并且只会为您提供给定索引的键(或值)。因此,为了获得所有键(或值),您需要将索引加一并继续直到WindowsError没有遇到。

This postmight help you for a detailed understanding on the same. The Github link for the code can be found in the post.

这篇文章可能会帮助您详细了解相同的内容。可以在帖子中找到代码的 Github 链接。