使用c#获取安装软件列表

本文关键字:软件 列表 安装 获取 使用 | 更新日期: 2023-09-27 18:14:26

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

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

我只从:

微软HKEY_LOCAL_MACHINE ' SOFTWARE ' Wow6432Node ' ' Windows ' CurrentVersion '卸载

但是我还需要Keys from:

微软HKEY_LOCAL_MACHINE ' SOFTWARE ' ' Windows ' CurrentVersion '卸载

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

编辑:Ok,我已经尝试检查应用程序是否安装在注册表和解决方案从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#获取安装软件列表

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

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

  1. SOFTWARE'Microsoft'Windows'CurrentVersion'Uninstall inside CurrentUser
  2. SOFTWARE'Microsoft'Windows'CurrentVersion'Uninstall inside LocalMachine
  3. SOFTWARE'Wow6432Node'Microsoft'Windows'CurrentVersion'Uninstall in 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;
}

看来你现在必须使用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());
                }
            }             
        } 

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

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

是否可以尝试添加引用"System. net . "

我认为更容易

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