I would like to move from a Powershell script to a C# app but I cannot get consistent results from getting a list of installed apps in the registry.
I have tried many different methods to read the registry, but they all result in the same missing items (which work with Powershell).
Working Powershell example:
$keys = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$software = Get-ItemProperty $keys | where-object {$_.displayname -ne $null} | Where-Object {$_.displayname -like "*Zip*" } | Select-Object DisplayName
Write-Output $software.DisplayName
#results:
.\test.ps1
7-Zip 16.04 (x64)
I have tried several examples (most from this site), here is one variant of a non-working C# example. I get results but not all items are in the list (see screenshot).
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey subKey1 = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
string[] subKeyNames = subKey1.GetSubKeyNames();
foreach (string keyName in subKeyNames) {
if (keyName.IndexOf("Zip") > 0) {
Console.WriteLine(keyName);
}
}
Console.ReadLine();
#results are empty
Another non-working c# example I have tried:
string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key)) {
foreach (string subkey_name in key.GetSubKeyNames()) {
using (RegistryKey subkey = key.OpenSubKey(subkey_name)) {
if (subkey.GetValue("DisplayName") != null) {
Console.WriteLine(subkey.GetValue("DisplayName"));
}
}
}
}
Console.ReadLine();
Here is a screen shot of the registry showing the example 7-Zip is listed there: