Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through registry entries

Tags:

c#

As suggested here, I need to iterate through entries in

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ 

to find out the installed path of my application. How to iterate so that I can find out the InstallLocation value given the DisplayName. How to do it efficiently in C#.

like image 681
Nemo Avatar asked Sep 22 '09 07:09

Nemo


People also ask

How to work with registry entries?

Because registry entries are properties of keys and, as such, cannot be directly browsed, we need to take a slightly different approach when working with them. Listing Registry Entries. There are many different ways to examine registry entries. The simplest way is to get the property names associated with a key.

How to set a single registry entry in a path?

Setting a Single Registry Entry. 1 Retrieve the current value of the Path entry using Get-ItemProperty. 2 Add the new value, separating it with a ;. 3 Use Set-ItemProperty with the specified key, entry name, and value to modify the registry entry.

How to retrieve the properties of the registry key?

Use the Get-Item cmdlet to retrieve the properties of the registry key. Pipe the registry properties through the ForEach-Object cmdlet. In the script block of the ForEach-Object cmdlet, use the Get-ItemProperty cmdlet to retrieve the property values. Return to the original working location by using the Pop-Location cmdlet.

How do I add a registry entry to multiple locations?

You can add a registry entry to multiple locations by specifying an array of values for the Path parameter: You can also overwrite a pre-existing registry entry value by adding the Force parameter to any New-ItemProperty command. To display the renamed value, add the PassThru parameter to the command.


2 Answers

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Whater\The\Key")) {   if (key != null)   {     foreach (string ValueOfName in key.GetValueNames())     {       try       {          bool Value = bool.Parse((string)key.GetValue(ValueOfName));       }       catch (Exception ex) {}     }  } } 

With a bool cast :D - so the string is expected to be True or False.

For the user registry hive use Registry.CurrentUser instead of Registry.LocalMachine

like image 42
rémy Avatar answered Sep 27 '22 18:09

rémy


Below is code to achieve your goal:

class Program {     static void Main(string[] args)     {         RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall");         foreach (var v in key.GetSubKeyNames())         {             Console.WriteLine(v);              RegistryKey productKey = key.OpenSubKey(v);             if (productKey != null)             {                 foreach (var value in productKey.GetValueNames())                 {                     Console.WriteLine("\tValue:" + value);                      // Check for the publisher to ensure it's our product                     string keyValue = Convert.ToString(productKey.GetValue("Publisher"));                     if (!keyValue.Equals("MyPublisherCompanyName", StringComparison.OrdinalIgnoreCase))                         continue;                      string productName = Convert.ToString(productKey.GetValue("DisplayName"));                     if (!productName.Equals("MyProductName", StringComparison.OrdinalIgnoreCase))                         return;                      string uninstallPath = Convert.ToString(productKey.GetValue("InstallSource"));                      // Do something with this valuable information                 }             }         }          Console.ReadLine();     } } 

Edit: See this method for a more comprehensive way to find an Applications Install Path, it demonstrates the using disposing as suggested in the comments. https://stackoverflow.com/a/26686738/495455

like image 145
Mike J Avatar answered Sep 27 '22 18:09

Mike J