Get installed applications in a system

后端 未结 14 1011
遥遥无期
遥遥无期 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 installs = new List();
    List keys = new List() {
      @"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 keys, List 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)
              { }
            }
          }
        }
      }
    }
    

提交回复
热议问题