繁体   English   中英

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

[英]Get installed software list using C#

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

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机器上运行。

编辑:好的,我已经尝试检查应用程序是否安装在注册表中,以及来自 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();

来源: 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)
                        { }
                    }
                }

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

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

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

我觉得更容易

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM