使用C#获取已安装的软件列表

时间:2014-07-23 11:20:15

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

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

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

我只从以下地址获得钥匙:

  

HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \微软\的Windows \ CurrentVersion \卸载

但我还需要以下的钥匙:

  

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();

6 个答案:

答案 0 :(得分:7)

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

解决方案是在注册表中搜索3个位置:

  1. SOFTWARE \ Microsoft \ Windows \ CurrentVersion \在CurrentUser中卸载
  2. SOFTWARE \ Microsoft \ Windows \ CurrentVersion \在LocalMachine中卸载
  3. SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \在LocalMachine中卸载
  4. 以下代码适合您的需要。

    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;
    }
    

答案 1 :(得分:1)

我使用了这段代码,它有点抽象,并且它检查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;
    }
}

答案 2 :(得分:0)

您可以尝试添加引用" System.Management"在您的项目中并在使用此代码后:

我觉得更容易

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

答案 3 :(得分:0)

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();  
   }  
}   

答案 4 :(得分:0)

看来您现在必须使用OpenBaseKey,代码im正在使用:

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());
                }
            }             
        } 

答案 5 :(得分:0)

所有答案均无法检索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)
                        { }
                    }
                }

            }