I wanted to copy the list of apps I've installed from control panel add/remove programs (programs and features - uninstall or change a program).
Of course that doesn't work (in win7). And well since I prefer C# over powershell, here's a quick console app to print the stuff from the registry keys.
static void Main(string[] args)
{
var apps = new List<InstalledApp>();
using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")) GetInstalledAppValues(key, apps);
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")) GetInstalledAppValues(key, apps);
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall")) GetInstalledAppValues(key, apps);
foreach (var a in apps.OrderBy(x => x.InstallDate.HasValue ? x.InstallDate.Value : DateTime.MinValue).ThenBy(x => x.Publisher).ThenBy(x => x.DisplayName))
{
Console.WriteLine("{0,-10} {1,-35} {2} ",a.InstallDate.HasValue ? a.InstallDate.Value.ToShortDateString(): "",a.Publisher,a.DisplayName);
}
}
private static void GetInstalledAppValues(RegistryKey key, List<InstalledApp> apps)
{
if (key == null) return;
foreach (String keyName in key.GetSubKeyNames())
{
var ia = new InstalledApp();
ia.DisplayName = (GetValue(key, keyName, "DisplayName") ?? "").Trim();
ia.DisplayVersion = GetValue(key, keyName, "DisplayVersion");
ia.InstallLocation = GetValue(key, keyName, "InstallLocation");
ia.InstallSource = GetValue(key, keyName, "InstallSource");
ia.Publisher = GetValue(key, keyName, "Publisher");
var dt = GetValue(key, keyName, "InstallDate");
if (dt != null)
{
DateTime res;
if (!DateTime.TryParseExact(dt, new string[] { "yyyyMMdd", "M/d/yyyy", "d" }, System.Threading.Thread.CurrentThread.CurrentCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out res))
DateTime.TryParseExact(dt, new string[] { "yyyyMMdd", "M/d/yyyy", "d" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces,out res);
if (res > DateTime.MinValue) ia.InstallDate = res;
}
if (!string.IsNullOrEmpty(ia.DisplayName)) apps.Add(ia);
}
}
private static string GetValue(RegistryKey key, String keyName, string valueName)
{
RegistryKey subkey = key.OpenSubKey(keyName);
return subkey.GetValue(valueName) as string;;
}
public class InstalledApp
{
public string DisplayName { get; set; }
public string DisplayVersion { get; set; }
public DateTime? InstallDate { get; set; }
public string InstallLocation { get; set; }
public string InstallSource { get; set; }
public string Publisher { get; set; }
}
Add your 2¢