如何在注册表项中搜索特定值
如何在注册表项中搜索特定值?
例如,我想在中搜索XXX
HKEY_CLASSES_ROOT\Installer\Products
C#中的任何代码示例都将受到赞赏,
谢谢
如果你不想依赖LogParser(虽然function强大):我会看一下Microsoft.Win32.RegistryKey
类( MSDN )。 使用OpenSubKey
打开HKEY_CLASSES_ROOT \ Installer \ Products,然后调用GetSubKeyNames
以获取子项的名称。
依次打开每一个,调用GetValue
获取您感兴趣的值(ProductName,我猜)并将结果与您要查找的结果进行比较。
在这里帮忙……
微软有一个很棒的(但不是很熟知的)工具 – 名为LogParser
它使用SQL引擎查询所有类型的基于文本的数据,如Registry,Filesystem,eventlog,AD等…要从C#中使用,您需要使用以下命令从Logparser.dll COM服务器构建Interop程序集(调整LogParser.dll路径)命令。
tlbimp "C:\Program Files\Log Parser 2.2\LogParser.dll" /out:Interop.MSUtil.dll
以下是一个小样本,演示了如何查询\ 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(); } } }