Get installed software list using C#

落爺英雄遲暮 提交于 2019-12-04 12:45:44

问题


I try to get a list of installed application keys:

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

I get only the Keys from:

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

But I need also the Keys from:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

My program should be able to run on a 64Bit and on a 32Bit machine.

edit: Ok I have tried Check if application is installed in registry and the solution from tHiNk_OuT_oF_bOx.

But nothing has solved the Problem!

The problem is i get exactly the same list for test and 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();

回答1:


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

The solution is to search for 3 places in registry:

  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

the code below suits you needs.

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



回答2:


Can you try to add the reference "System.Management" in your project and after use this code:

I think more easy

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



回答3:


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:


It seems you now have to use OpenBaseKey, there is the code im using:

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:


All answers fail to retrieve every installed application on Windows 10. The codes above only display most used installed apps NOT all installed apps. So I found this solution:

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)
                        { }
                    }
                }

            }


来源:https://stackoverflow.com/questions/24909108/get-installed-software-list-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!