首页 文章

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

提问于
浏览
4

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

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

4 回答

  • 0

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

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

    在CurrentUser中

    • SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall
      在LocalMachine中
    • SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall
      在LocalMachine中
    • SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall

    以下代码适合您的需要 .

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

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

    我觉得更容易

    ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
    foreach (ManagementObject mo in mos.Get())
    {
         Console.WriteLine(mo["Name"]);
    }
    
  • 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();  
       }  
    }
    
  • 0

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

相关问题