10

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

例如我想搜索 XXX in

HKEY_CLASSES_ROOT\Installer\Products

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

谢谢

4

4 回答 4

19

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

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

于 2008-11-17T14:59:11.910 回答
11

在这里帮忙...

微软有一个很棒的(但不是众所周知的)工具——叫做LogParser

它使用 SQL 引擎来查询所有类型的基于文本的数据,如注册表、文件系统、事件日志、AD 等...要从 C# 中使用,您需要使用以下命令从 Logparser.dll COM 服务器构建一个互操作程序集(调整 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();
}
}
}
于 2008-11-17T10:26:33.193 回答
2

此方法将在指定的注册表项中搜索包含指定值的第一个子项。如果找到键,则返回指定的值。Searchign 只有一级深。如果您需要更深入的搜索,那么我建议修改此代码以利用递归。搜索区分大小写,但如果需要,您可以再次修改它。

private string SearchKey(string keyname, string data, string valueToFind, string returnValue)
{
    RegistryKey uninstallKey = Registry.LocalMachine.OpenSubKey(keyname);
    var programs = uninstallKey.GetSubKeyNames();

    foreach (var program in programs)
    {
        RegistryKey subkey = uninstallKey.OpenSubKey(program);
        if (string.Equals(valueToFind, subkey.GetValue(data, string.Empty).ToString(), StringComparison.CurrentCulture))
        {
            return subkey.GetValue(returnValue).ToString();
        }
    }

    return string.Empty;
}

示例用法

// This code will find the version of Chrome (32 bit) installed
string version = this.SearchKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall", "DisplayName", "Google Chrome", "DisplayVersion");
于 2020-09-23T13:38:33.123 回答
0

@Caltor 你的解决方案给了我我正在寻找的答案。我欢迎改进或不涉及注册表的完全不同的解决方案。我正在使用已加入 Azure AD 的设备在 Windows 10 上使用企业应用程序。我希望/需要在 UWP 应用中为设备和 HoloLens 2 使用 Windows Hello。我的问题是从 Windows 10 获取 AAD userPrincipal 名称。经过几天搜索和尝试大量代码后,我在 Windows 注册表中搜索了当前用户密钥中的 AAD 帐户并找到了它。通过一些研究,这些信息似乎在一个特定的键中。因为您可以加入多个目录,所以可能有多个条目。我并没有试图解决这个问题,而是使用 AAD 租户 ID 完成的。我只需要 AAD userPrincipal 名称。我的解决方案对返回列表进行去重复,以便我拥有一个唯一的 userPrincipal 名称列表。应用程序用户可能必须选择一个帐户,这对于 HoloLens 来说也是可以容忍的。

using Microsoft.Win32;
using System.Collections.Generic;
using System.Linq;

namespace WinReg
{
  public class WinRegistryUserFind
  {
    // Windows 10 apparently places Office/Azure AAD in the registry at this location
    // each login gets a unique key in the registry that ends with the aadrm.com and the values
    // are held in a key named Identities and the value we want is the Email data item.
    const string regKeyPath = "SOFTWARE\\Classes\\Local Settings\\Software\\Microsoft\\MSIPC";
    const string matchOnEnd = "aadrm.com";
    const string matchKey = "Identities";
    const string matchData = "Email";

    public static List<string> GetAADuserFromRegistry()
    {
      var usersFound = new List<string>();
      RegistryKey regKey = Registry.CurrentUser.OpenSubKey(regKeyPath);
      var programs = regKey.GetSubKeyNames();
      foreach (var program in programs)
      {
        RegistryKey subkey = regKey.OpenSubKey(program);
        if(subkey.Name.EndsWith(matchOnEnd))
        {
          var value = (subkey.OpenSubKey(matchKey) != null)? (string)subkey.OpenSubKey(matchKey).GetValue(matchData): string.Empty;
          if (string.IsNullOrEmpty(value)) continue;
          if((from user in usersFound where user == value select user).FirstOrDefault() == null)
            usersFound.Add(value) ;
        }
      }

      return usersFound;
    }
  }
}
于 2020-10-11T17:49:44.993 回答