0

I need to query the registry through keywords and return all matching registry addresses, but I cannot access the following branches with this function。

HKEY_LOCAL_MACHINE\SOFTWARE\abc\test

HKEY_LOCAL_MACHINE\SYSTEM\test

WHY? I don't know where to write the question。 Thanks.

keywork="test"

I am in VS, have used administrator rights.

public static DataTable SearchRegistry(string keyword)
        {
            // List<string> matchedAddresses = new List<string>();
            DataTable dataTable = new DataTable("MyTable");
            dataTable.Columns.Add("Name", typeof(string));
            dataTable.Columns.Add("HOST", typeof(string));


            try
            {
                SearchRegistryKeys(Registry.ClassesRoot, keyword, "", dataTable);

                SearchRegistryKeys(Registry.CurrentUser, keyword, "", dataTable);

                SearchRegistryKeys(Registry.LocalMachine, keyword, "", dataTable);

                SearchRegistryKeys(Registry.Users, keyword, "", dataTable);

                SearchRegistryKeys(Registry.CurrentConfig, keyword, "", dataTable);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return dataTable;
        }

 

 public static void SearchRegistryKeys(RegistryKey key, string keyword, string currentPath, DataTable dataTable)
        {
            string registername = key.Name;

                foreach (string subKeyName in key.GetSubKeyNames())
                {
                    using (RegistryKey subKey = key.OpenSubKey(subKeyName))
                    {
                        if (subKey != null)
                        {
                            if (subKeyName.ToLower().Contains(keyword) )
                            {
                               // dataTable.Rows.Add(subKeyName);
                                dataTable.Rows.Add($"{currentPath}\\{subKeyName}", "");
                            }

                            SearchRegistryKeys(subKey, keyword, $"{currentPath}\\{subKeyName}", dataTable);
                        }
                    }
                }
        }

【erro msg】:The requested registry access is not allowed

enter image description here enter image description here

enter image description here

enter image description here

gong go
  • 1
  • 3

1 Answers1

0

There were two issues in you code. One is that some registry is unaccessible even if with admin right. The other is that GetSubKeyNames might return null. The following code should work. Please make sure that your project is x64 or "Any CPU" (With "Prefer 32-bit" unchecked) architecture. If the project is x86, "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node" will be searched instead of "HKEY_LOCAL_MACHINE\SOFTWARE".

public static void SearchRegistryKeys(RegistryKey key, string keyword, string currentPath, DataTable dataTable)
    {
        try
        {
            string registername = key.Name;

            //GetSubKeyNames might return null
            var subKeyNames = key.GetSubKeyNames();
            if (subKeyNames == null)
            {
                return;
            }

            foreach (string subKeyName in subKeyNames)
            {
                try
                {
                    using (RegistryKey subKey = key.OpenSubKey(subKeyName))
                    {
                        if (subKey != null)
                        {
                            if (subKeyName.ToLower().Contains(keyword))
                            {
                                dataTable.Rows.Add($"{currentPath}\\{subKeyName}", "");
                            }

                            SearchRegistryKeys(subKey, keyword, $"{currentPath}\\{subKeyName}", dataTable);
                        }
                    }
                }
                catch (SecurityException)
                {
                    //Some registry is unaccessible
                    Console.WriteLine($"Cannot access {currentPath}\\{subKeyName}");
                }
                catch
                {
                    throw;
                }
            }
        }
        catch (SecurityException)
        {
            //Some registry is unaccessible
            Console.WriteLine($"Cannot access {currentPath}");
        }
        catch
        {
            throw;
        }
    }

Running result

ioi
  • 31
  • 2
  • HI: I get an error like this after executing 【The requested registry access is not allowed】 – gong go Jun 08 '23 at 14:25
  • @gonggo Some registry required System authority and Administrator is not enough. There registry do not have the data that you required. Leave Catch empty as my code in SearchRegistryKeys will solve the issue. – ioi Jun 09 '23 at 06:51
  • So what permissions do you need to add besides System and Administrator? If you just empty the Catch. Now I have encountered such a problem. You can see through the registry in Windows that the searched value is still there, but the keyword cannot be found with the code. – gong go Jun 09 '23 at 08:21
  • @gonggo The code worked on my computer. Administrator right should be enough in your project. You might have input the wrong "keyword". If you need more help, please export your registry and provide the real "keyword". – ioi Jun 14 '23 at 09:28
  • I updated a picture. The system I use is win 11 enterprise edition – gong go Jun 16 '23 at 01:28
  • @gonggo I had updated my code and results picture. It worked on windows 11. If it does not work on your computer. Please see the console log to find whether any parent node of "Computer\HKEY_LOCAL_MACHINE\SOFTWARE\testabcdd" is not accessible. And change the corresponded permission. – ioi Jun 16 '23 at 08:16
  • It's amazing, I changed a few machines here, and they all have the same problem Same thing with a fresh system reinstall. no solution anymore – gong go Jun 19 '23 at 06:43
  • @gonggo Please make sure that your project is x64 or "Any CPU" architecture. If the project is x86, "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node" will be searched instead of "HKEY_LOCAL_MACHINE\SOFTWARE". – ioi Jun 19 '23 at 07:39
  • YES ,i found the problem Attributes generate [Preferred 32-bit] is checked, I unchecked it, and I can search for it。you can see my new pictures – gong go Jun 19 '23 at 14:27
  • @gonggo Great to hear that the problem was solved. Please mark the answer as accepted. – ioi Jun 20 '23 at 01:45