C# 如何在注册表项中搜索特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/295257/
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-08-03 22:02:33  来源:igfitidea点击:

How to search for specific value in Registry keys

c#registry

提问by

How can I search for specific value in the registry keys?

如何在注册表项中搜索特定值?

For example I want to search for XXX in

例如我想在 XXX 中搜索

HKEY_CLASSES_ROOT\Installer\Products

any code sample in C# will be appreciated,

将不胜感激 C# 中的任何代码示例,

thanks

谢谢

回答by Galwegian

Help here...

在这里帮助...

Microsoft has a great (but not well known) tool for this - called LogParser

微软有一个很棒的(但不是众所周知的)工具 - 称为LogParser

It uses a SQL engine to query all kind of text based data like the Registry, the Filesystem, the eventlog, AD etc... To be usable from C#, you need to build an Interop Assembly from the Logparser.dll COM server using following (adjust LogParser.dll path) command.

它使用 SQL 引擎来查询所有类型的基于文本的数据,如注册表、文件系统、事件日志、AD 等...要从 C# 使用,您需要使用以下方法从 Logparser.dll COM 服务器构建一个互操作程序集(调整 LogParser.dll 路径)命令。

tlbimp "C:\Program Files\Log Parser 2.2\LogParser.dll"
/out:Interop.MSUtil.dll

Following is a small sample, that illustrates how to query for the Value 'VisualStudio' in the \HKLM\SOFTWARE\Microsoft tree.

以下是一个小示例,它说明了如何在 \HKLM\SOFTWARE\Microsoft 树中查询值“VisualStudio”。

using System;
using System.Runtime.InteropServices;
using LogQuery = Interop.MSUtil.LogQueryClass;
using RegistryInputFormat = Interop.MSUtil.COMRegistryInputContextClass;
using RegRecordSet = Interop.MSUtil.ILogRecordset;

class Program
{
public static void Main()
{
RegRecordSet rs = null;
try
{
LogQuery qry = new LogQuery();
RegistryInputFormat registryFormat = new RegistryInputFormat();
string query = @"SELECT Path from \HKLM\SOFTWARE\Microsoft where
Value='VisualStudio'";
rs = qry.Execute(query, registryFormat);
for(; !rs.atEnd(); rs.moveNext())
Console.WriteLine(rs.getRecord().toNativeString(","));
}
finally
{
rs.close();
}
}
}

回答by Arnout

In case you don't want to take a dependency on LogParser (as powerful as it is): I would take a look at the Microsoft.Win32.RegistryKeyclass (MSDN). Use OpenSubKeyto open up HKEY_CLASSES_ROOT\Installer\Products, and then call GetSubKeyNamesto, well, get the names of the subkeys.

如果您不想依赖 LogParser(尽管它很强大):我会看看这个Microsoft.Win32.RegistryKey类(MSDN)。使用OpenSubKey打开HKEY_CLASSES_ROOT\Installer\Products,然后调用GetSubKeyNames,好,获取子项的名称。

Open up each of those in turn, call GetValuefor the value you're interested in (ProductName, I guess) and compare the result to what you're looking for.

依次打开其中的每一个,调用GetValue您感兴趣的值(我猜是 ProductName)并将结果与​​您要查找的值进行比较。