/// <summary> /// Lists applications installed on the system /// </summary> /// <returns>ArrayList of AppInstallInfo instances</returns> public static ArrayList GetInstalledApps(bool reload = false) { if (!reload && SystemAppCatalog != null) return SystemAppCatalog; try { SystemAppCatalog = new ArrayList(); string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; string uninstallKey32 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; for (var i = 0; i < 4; i++) { // We're doing four passes: first pass from LocalMachine hive, second pass from CurrentUser // Third pass: LocalMachine Wow6432Node, Fourth pass: CurrentUser Wow6432Node // Normally most apps should be listed in LocalMachine, but some apps such as Torch (web browser) // for some reason only appear in CurrentUser // Try it, if it fails, continue to the next pass try { RegistryKey rk; // Set folder for each pass if (i == 1) rk = Registry.CurrentUser.OpenSubKey(uninstallKey); else if (i == 2) rk = Registry.LocalMachine.OpenSubKey(uninstallKey32); else if (i == 3) rk = Registry.CurrentUser.OpenSubKey(uninstallKey32); else rk = Registry.LocalMachine.OpenSubKey(uninstallKey); foreach (string skName in rk.GetSubKeyNames()) { using (RegistryKey sk = rk.OpenSubKey(skName)) { try { if (sk.GetValueKind("DisplayName") == RegistryValueKind.String) { AppInstallInfo app = new AppInstallInfo(""); app.KeyName = skName; app.DisplayName = (string)sk.GetValue("DisplayName");// +skName; app.DisplayVersion = ""; // Attempt to get the version try { app.DisplayVersion = (string)sk.GetValue("DisplayVersion"); } catch { app.DisplayVersion = ""; } // If there is a tag for the major version number var version = -1; try { if (sk.GetValueKind("MajorVersion") == RegistryValueKind.String) version = int.Parse((string)sk.GetValue("MajorVersion")); else version = (int)sk.GetValue("MajorVersion"); } catch { } if (version == -1) { try { if (sk.GetValueKind("VersionMajor") == RegistryValueKind.String) version = int.Parse((string)sk.GetValue("VersionMajor")); else version = (int)sk.GetValue("VersionMajor"); } catch { } } // Attempt to figure out the version by looking at DisplayVersion if (version == -1 && app.DisplayVersion != null) { var parts = app.DisplayVersion.Split('.'); if (parts.Length > 0) { var possibleMajorVersion = parts[0]; try { version = int.Parse(possibleMajorVersion); } catch { } } } if (version == -1) version = 0; app.MajorVersionNumber = version; app.Supported = true; // Is it a driver? if (app.DisplayName.Contains("Windows Driver Package")) continue; if (app.DisplayName.Contains("Update for Microsoft")) continue; if (app.DisplayName.Contains("Microsoft") && app.DisplayName.Contains("MUI")) continue; // Check whether the app is supported or not app.CheckSupport(); // Already there? if (!AlreadyInCatalog(app)) SystemAppCatalog.Add(app); } } catch { //MessageBox.Show("STEP 2 " + ex.ToString()); } } } } catch { // Nothing, continue //MessageBox.Show("STEP 1 "+ex.ToString() ); } } return SystemAppCatalog; } catch { return null; } }
/// <summary> /// Checks whether given app is already listed in the catalog (checks display name + version) /// This can occur when both 32-bit and 64-bit versions are installed on the system /// </summary> static bool AlreadyInCatalog(AppInstallInfo app) { if (SystemAppCatalog == null) return false; foreach (AppInstallInfo inf in SystemAppCatalog) { if (inf.DisplayName.ToLower() == app.DisplayName.ToLower() && inf.DisplayVersion.ToLower() == app.DisplayVersion.ToLower()) return true; } return false; }