How to search for specific value in Registry keys

后端 未结 4 764
暗喜
暗喜 2020-12-15 12:19

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

For example I want to search for XXX in

HKEY_CLASSES_ROOT\\Installer\\Products
         


        
4条回答
  •  孤街浪徒
    2020-12-15 12:41

    @Caltor your solution gave me the answer I was looking for. I welcome improvements or a completely different solution that does not involve the registry. I am working with enterprise applications on Windows 10 with devices joined to Azure AD. I want/need to use Windows Hello for devices and for HoloLens 2 in a UWP app. My problem has been getting the AAD userPrincipal name from Windows 10. After a couple days searching and trying lots of code I searched the Windows Registry for my AAD account in the Current User key and found it. With some research it appears that this information is in a specific key. Because you can be joined to multiple directories there may be more than one entry. I was not trying to solve that issue, that is done with the AAD tenant Id. I just needed the AAD userPrincipal name. My solution de-dups the return list so that I have a list of unique userPrincipal names. App users may have to select an account, this is tolerable for even 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 GetAADuserFromRegistry()
        {
          var usersFound = new List();
          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;
        }
      }
    }
    

提交回复
热议问题