Get installed applications in a system

后端 未结 14 996
遥遥无期
遥遥无期 2020-11-22 08:28

How to get the applications installed in the system using c# code?

相关标签:
14条回答
  • 2020-11-22 09:25

    While the accepted solution works, it is not complete. By far.

    If you want to get all the keys, you need to take into consideration 2 more things:

    x86 & x64 applications do not have access to the same registry. Basically x86 cannot normally access x64 registry. And some applications only register to the x64 registry.

    and

    some applications actually install into the CurrentUser registry instead of the LocalMachine

    With that in mind, I managed to get ALL installed applications using the following code, WITHOUT using WMI

    Here is the code:

    List<string> installs = new List<string>();
    List<string> keys = new List<string>() {
      @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
      @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    };
    
    // The RegistryView.Registry64 forces the application to open the registry as x64 even if the application is compiled as x86 
    FindInstalls(RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64), keys, installs);
    FindInstalls(RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64), keys, installs);
    
    installs = installs.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
    installs.Sort(); // The list of ALL installed applications
    
    
    
    private void FindInstalls(RegistryKey regKey, List<string> keys, List<string> installed)
    {
      foreach (string key in keys)
      {
        using (RegistryKey rk = regKey.OpenSubKey(key))
        {
          if (rk == null)
          {
            continue;
          }
          foreach (string skName in rk.GetSubKeyNames())
          {
            using (RegistryKey sk = rk.OpenSubKey(skName))
            {
              try
              {
                installed.Add(Convert.ToString(sk.GetValue("DisplayName")));
              }
              catch (Exception ex)
              { }
            }
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-22 09:31

    My requirement is to check if specific software is installed in my system. This solution works as expected. It might help you. I used a windows application in c# with visual studio 2015.

     private void Form1_Load(object sender, EventArgs e)
            {
    
                object line;
                string softwareinstallpath = string.Empty;
                string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
                using (var baseKey = Microsoft.Win32.RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
                {
                    using (var key = baseKey.OpenSubKey(registry_key))
                    {
                        foreach (string subkey_name in key.GetSubKeyNames())
                        {
                            using (var subKey = key.OpenSubKey(subkey_name))
                            {
                                line = subKey.GetValue("DisplayName");
                                if (line != null && (line.ToString().ToUpper().Contains("SPARK")))
                                {
    
                                    softwareinstallpath = subKey.GetValue("InstallLocation").ToString();
                                    listBox1.Items.Add(subKey.GetValue("InstallLocation"));
                                    break;
                                }
                            }
                        }
                    }
                }
    
                if(softwareinstallpath.Equals(string.Empty))
                {
                    MessageBox.Show("The Mirth connect software not installed in this system.")
                }
    
    
    
                string targetPath = softwareinstallpath + @"\custom-lib\";
                string[] files = System.IO.Directory.GetFiles(@"D:\BaseFiles");
    
                // Copy the files and overwrite destination files if they already exist. 
                foreach (var item in files)
                {
                    string srcfilepath = item;
                    string fileName = System.IO.Path.GetFileName(item);
                    System.IO.File.Copy(srcfilepath, targetPath + fileName, true);
                }
                return;
    
            }
    
    0 讨论(0)
提交回复
热议问题