Cloud Stack Ninja

I am trying to read the value of a name from the Registry using C#. The Registry Path is 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion'

The name I want the value of is 'RegisteredOrganization'

If I use REGEDIT, I can goto the specified path and see the name and its value.

I want to do the same but using c#. I use the following code:

           string rPath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
            string key = "RegisteredOrganization";

            try
            {
                using (RegistryKey regkey = Registry.LocalMachine.OpenSubKey(rPath))
                {
                    object nameValue = regkey.GetValue(key);
                    if (nameValue != null)
                    { Console.WriteLine($"{key} = {nameValue}"); }
                    else
                    { Console.WriteLine($"{key} not found!"); }
                }
            }
            catch
            {
                Console.WriteLine($"Error opening key {rPath}");
            }

The result is: RegisteredOrganization not found! Running the program with elevated permission gives me the same results

If I rewrote the program to show the keys in the path it will only show 23 names from the 30 REGEDIT will show.

Can someone give me an explanation for this discrepantie or what I should change in my code to make it work?



Read more here: https://stackoverflow.com/questions/64405086/difference-in-result-regedit-query-and-c-sharp-program-query-using-registry-clas

Content Attribution

This content was originally published by cfwl at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: