C# Count registry keys existing using a partial value

Microsoft.Win32.RegistryKey registryPath = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Testing");

Microsoft.Win32.RegistryKey entryKey = registryPath.OpenSubKey("Entry Point");

I have a lot of keys in Testing, in the format: "Entry Point 011", "Entry Point 123" - so Entry Point with random numbers after it.

Would I be able to search the registryPath variable above and get a count of the number of keys containing the "Entry Point" keyword? Assuming that there are also other keys existing without this keyword.

At the moment I have been using a for loop and looping for all possible combinations to get a count of all the keys, checking if the key exists or not, but as there are keys as high as "Entry Point 9000" having a for loop execute 9000 times is very inefficient.

 for (int i = 0; i <= highestEntryPointValue; i++)
 {
     Microsoft.Win32.RegistryKey entryKey = steamApps64.OpenSubKey("Entry Point " + Convert.ToString(i));

     if (entryKey != null)
     {
         count++;
     }
 }

Answers


Microsoft.Win32.RegistryKey has a method called GetSubKeyNames() which returns an array of string with names.

string[] keys = registryPath.GetSubKeyNames();

now you can loop on these keys and check their name without opening in vain the registry

var subKeys = Array.FindAll(keys, key => key.Substring(0, 11) == "Entry Point"));
int count = subKeys.Lenght;
foreach(string s in subKeys)
.....

This example is not efficient as it will count (for eg) till 9000 but what if there will be only 3 key EntryPoint 011,EntryPoint 123, EntryPoint 9000.

One Line answer

int count = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Testing").GetSubKeyNames().Where(s => s.StartsWith("EntryPoint")).Count();

Need Your Help

php/pear package for "remember me on this computer"

php cookies pear

I know this is usually done with cookies, but I am getting unpredictable results on different browsers and decided to look for a pear package that focuses on this. Just wondering if there's a pack...