0

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: screenshot

  • 2
    Change to `regKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);` and see if that makes a difference, if so its [Wow6432Node redirection](https://stackoverflow.com/questions/13728491/opensubkey-returns-null-for-a-registry-key-that-i-can-see-in-regedit-exe). – Alex K. Jun 16 '17 at 15:37
  • The first one will not work because you only go through folders. The second one is likely a 32-64 bit issue. Compile as a 64 bit program, remove "prefer 32 bit" setting and it might work. – Thomas Weller Jun 16 '17 at 16:00
  • Thanks Alex, that did the trick! – Tim Beacham Jun 16 '17 at 16:11
  • 1
    I don't think Alex wants to give an answer. He pointed to another answer which gives the solution. You should now see a message at the top of your question (refresh needed, maybe) that you can accept and this problem will be marked as solved. – Thomas Weller Jun 16 '17 at 16:13

0 Answers0