【问题标题】:Get installed software list using C#使用 C# 获取已安装的软件列表
【发布时间】:2014-07-23 11:20:14
【问题描述】:

我尝试获取已安装应用程序密钥的列表:

RegistryKey RegKeyUninstallList = Registry.LocalMachine;
string strUninstallList = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
string[] test = RegKeyUninstallList.OpenSubKey(strUninstallList).GetSubKeyNames();

我只从以下位置获取密钥:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

但我还需要以下密钥:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

我的程序应该能够在64Bit32Bit 机器上运行。

编辑:好的,我已经尝试过 Check if application is installed in registry 和来自 tHiNk_OuT_oF_bOx 的解决方案。

但问题没有解决!

问题是我得到了完全相同的 test 和 test2 列表:

RegistryKey RegKeyUninstallList = Registry.LocalMachine;
string strUninstallList = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
string strUninstallList2 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
string[] test = RegKeyUninstallList.OpenSubKey(strUninstallList).GetSubKeyNames();
string[] test2 = RegKeyUninstallList.OpenSubKey(strUninstallList2).GetSubKeyNames();

【问题讨论】:

标签: c# x86-64 regedit installed-applications


【解决方案1】:

来源:http://social.msdn.microsoft.com/Forums/en-US/94c2f14d-c45e-4b55-9ba0-eb091bac1035/c-get-installed-programs

解决方法是在注册表中搜索3个地方:

  1. 在 CurrentUser 中的 SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
  2. 在 LocalMachine 中安装 SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
  3. SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\在 LocalMachine 中卸载

下面的代码适合你的需要。

public static bool IsApplicationInstalled(string p_name)
{
    string displayName;
    RegistryKey key;

    // search in: CurrentUser
    key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (String keyName in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(keyName);
        displayName = subkey.GetValue("DisplayName") as string;
        if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
        {
            return true;
        }
    }

    // search in: LocalMachine_32
    key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (String keyName in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(keyName);
        displayName = subkey.GetValue("DisplayName") as string;
        if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
        {
            return true;
        }
    }

    // search in: LocalMachine_64
    key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (String keyName in key.GetSubKeyNames())
    {
        RegistryKey subkey = key.OpenSubKey(keyName);
        displayName = subkey.GetValue("DisplayName") as string;
        if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
        {
            return true;
        }
    }

    // NOT FOUND
    return false;
}

【讨论】:

  • 不要使用Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node...")。请改用RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, is64bit ? RegistryView.Registry64 : RegistryView.Registry32); ;)
  • @Xaruth 谢谢,它适用于 `´RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, is64bit ? RegistryView.Registry64 : RegistryView.Registry32)´
  • 请求的注册表访问不允许:(请提供任何解决方案?
  • @Xaruth 当我们使用您的代码时,它会显示安全异常
【解决方案2】:

看来你现在必须使用 OpenBaseKey,我正在使用代码:

List<string> gInstalledSoftware
        void GetInstalledSoftwareList()
        {
            string displayName;

            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", false))
            {                
                foreach (String keyName in key.GetSubKeyNames())
                {
                    RegistryKey subkey = key.OpenSubKey(keyName);
                    displayName = subkey.GetValue("DisplayName") as string;
                    if (string.IsNullOrEmpty(displayName))
                        continue;

                    gInstalledSoftware.Add(displayName.ToLower());
                }
            }

            //using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", false))
            using (var localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                var key = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", false);
                foreach (String keyName in key.GetSubKeyNames())
                {
                    RegistryKey subkey = key.OpenSubKey(keyName);
                    displayName = subkey.GetValue("DisplayName") as string;
                    if (string.IsNullOrEmpty(displayName))
                        continue;

                    gInstalledSoftware.Add(displayName.ToLower());
                }
            }

            using (var localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
            {
                var key = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", false);
                foreach (String keyName in key.GetSubKeyNames())
                {
                    RegistryKey subkey = key.OpenSubKey(keyName);
                    displayName = subkey.GetValue("DisplayName") as string;
                    if (string.IsNullOrEmpty(displayName))
                        continue;

                    gInstalledSoftware.Add(displayName.ToLower());
                }
            }

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall",false))
            {
                foreach (String keyName in key.GetSubKeyNames())
                {
                    RegistryKey subkey = key.OpenSubKey(keyName);
                    displayName = subkey.GetValue("DisplayName") as string;
                    if (string.IsNullOrEmpty(displayName))
                        continue;

                    gInstalledSoftware.Add(displayName.ToLower());
                }
            }             
        } 

【讨论】:

    【解决方案3】:

    所有答案都无法检索 Windows 10 上每个已安装的应用程序。以上代码仅显示最常用的已安装应用程序并非所有已安装应用程序。所以我找到了这个解决方案:

    string appPATH = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
                using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(appPATH))
                {
                    foreach (string skName in rk.GetSubKeyNames())
                    {
                        using (RegistryKey sk = rk.OpenSubKey(skName))
                        {
                            try
                            {
    
                                //Get App Name
                                var displayName = sk.GetValue("DisplayName");
                                //Get App Size
                                var size = sk.GetValue("EstimatedSize");
    
                                string item;
                                if (displayName != null)
                                {
                                    if (size != null)
                                        item = displayName.ToString();
                                    else
                                    {
                                        item = displayName.ToString();
                                        if (item.Contains(""))
                                        MessageBox.Show(displayName.ToString());
    
                                    }
    
                                }
                            }
                            catch (Exception ex)
                            { }
                        }
                    }
    
                }
    

    【讨论】:

      【解决方案4】:

      我使用了这段代码,它有点抽象,它会检查 32/64 位操作系统。为了简单起见,它还使用了 C#V8 功能。

      public static bool CheckAppInstallation(string programName)
      {
          bool result = false;
          string uninstallKey1 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
          string uninstallKey2 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
          result |= CheckInAddress(uninstallKey1, RegistryHive.LocalMachine, programName);
          result |= CheckInAddress(uninstallKey1, RegistryHive.CurrentUser, programName);
          result |= CheckInAddress(uninstallKey2, RegistryHive.LocalMachine, programName);
          return result;
      
          static bool CheckInAddress(string address, RegistryHive hive, string programName)
          {
              var view = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
              using var localKey = RegistryKey.OpenBaseKey(hive, view).OpenSubKey(address);
              foreach (var subKey in localKey.GetSubKeyNames().Select(keyName => localKey.OpenSubKey(keyName)))
                  if (subKey.GetValue("DisplayName") is string displayName && displayName.Contains(programName))
                      return true;
              return false;
          }
      }
      

      【讨论】:

        【解决方案5】:
        using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.Data;
        using System.Diagnostics;
        using Microsoft.Win32;
        
        
        public void GetInstalledApps()  
        {  
           string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";  
           using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))  
           {  
               foreach (string skName in rk.GetSubKeyNames())  
               {  
                   using (RegistryKey sk = rk.OpenSubKey(skName))  
                   {  
                       try  
                       {    
                          listBox1.Items.Add(sk.GetValue("DisplayName"));                             
                       }  
                       catch (Exception ex)  
                       { }  
                   }  
               }  
               label1.Text = listBox1.Items.Count.ToString();  
           }  
        }   
        

        【讨论】:

        • using Microsoft.VisualBasic;??
        • 只是在 vb.net 中做并转换为 c# 这就是原因
        【解决方案6】:

        您能否尝试在您的项目中添加引用“System.Management”并在使用此代码后:

        我觉得更容易

        ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
        foreach (ManagementObject mo in mos.Get())
        {
             Console.WriteLine(mo["Name"]);
        }
        

        【讨论】:

        • 您好,这将仅返回 Microsoft 的已安装产品列表,而不是全部。
        猜你喜欢
        • 2020-11-14
        • 1970-01-01
        • 2011-09-02
        • 1970-01-01
        • 2016-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多