若C#的程序都是32位的,访问注册表的时候,会访问HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\, 而访问不到HKEY_LOCAL_MACHINE\SOFTWARE
适用版本:.NET 4.0及更高版本
public static Dictionary<string, string> GetInstalledList()
{Dictionary<string, string> softList = new Dictionary<string, string>();try{// search in: CurrentUserRegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")){if (!softList.ContainsKey(item.Key)){softList.Add(item.Key, item.Value);}}localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")){if (!softList.ContainsKey(item.Key)){softList.Add(item.Key, item.Value);}}// search in: LocalMachine_32localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")){if (!softList.ContainsKey(item.Key)){softList.Add(item.Key, item.Value);}}localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")){if (!softList.ContainsKey(item.Key)){softList.Add(item.Key, item.Value);}}// search in: LocalMachine_64localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall")){if (!softList.ContainsKey(item.Key)){softList.Add(item.Key, item.Value);}}localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);foreach (var item in GetInstallSoftList(localKey, @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall")){if (!softList.ContainsKey(item.Key)){softList.Add(item.Key, item.Value);}}}catch (Exception ex){logger.Error(ex);}return softList;
}private static Dictionary<string, string> GetInstallSoftList(RegistryKey localKey, string subKey)
{Dictionary<string, string> softList = new Dictionary<string, string>();RegistryKey key = localKey.OpenSubKey(subKey);if (key != null){foreach (String keyName in key.GetSubKeyNames()){RegistryKey subkey = key.OpenSubKey(keyName);string displayName = subkey.GetValue("DisplayName") as string;var version = GetSoftVersion(subkey);if (displayName != null && !softList.ContainsKey(displayName)){softList.Add(displayName, version == null ? "" : version.ToString().Replace(",", ".").Replace(" ", ""));}}}return softList;
}private static string GetSoftVersion(RegistryKey key)
{if (key == null){return null;}var version = key.GetValue("DisplayVersion");if (version != null && !string.IsNullOrEmpty(version.ToString())){return version.ToString();}var majorVersion = key.GetValue("MajorVersion");var minorVersion = key.GetValue("MinorVersion");if (majorVersion != null && minorVersion != null && !string.IsNullOrEmpty(majorVersion.ToString()) && !string.IsNullOrEmpty(minorVersion.ToString())){return majorVersion.ToString() + "." + minorVersion;}majorVersion = key.GetValue("VersionMajor");minorVersion = key.GetValue("VersionMinor");if (majorVersion != null && minorVersion != null && !string.IsNullOrEmpty(majorVersion.ToString()) && !string.IsNullOrEmpty(minorVersion.ToString())){return majorVersion.ToString() + "." + minorVersion;}return null;
}