using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp4
{
public class Info : IComparable
{
public string DisplayName { get; set; }
public string DisplayVersion { get; set; }
public string Publisher { get; set; }
public int CompareTo(object obj)
{
if (obj == null) return 1;
if (obj is Info otherTemperature)
return string.Compare(DisplayName, otherTemperature.DisplayName, StringComparison.Ordinal);
throw new ArgumentException("Object is not a string");
}
}
internal class Program
{
static void Main(string[] args)
{
List<Info> installs = new List<Info>();
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.DisplayName)).Distinct().ToList();
installs.Sort();
}
private static void FindInstalls(RegistryKey regKey, IEnumerable<string> keys, ICollection<Info> installed)
{
foreach (var key in keys)
{
using (var rk = regKey.OpenSubKey(key))
{
if (rk == null)
{
continue;
}
foreach (var skName in rk.GetSubKeyNames())
{
using (var sk = rk.OpenSubKey(skName))
{
if (sk == null) continue;
try
{
var displayName = Convert.ToString(sk.GetValue("DisplayName"));
var displayVersion = Convert.ToString(sk.GetValue("DisplayVersion"));
var publisher = Convert.ToString(sk.GetValue("Publisher"));
installed.Add(new Info
{
DisplayName = displayName,
DisplayVersion = displayVersion,
Publisher = publisher
});
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
}
}
}
}