Hi all ! I have a code which search registry key value in specific key path, not all registry.
In registry key SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}
find all keys with 0000 , 0001 , 0002 , 0003 and so on at the end of registry key. {4D36E972-E325-11CE-BFC1-08002BE10318} . In each key ( for example 0007 ) are subkey called NetCfgInstanceId which holds network interface
card ID value , like this {C80949A4-CEDA-4F29-BFE2-059856D7F745} . If finds value, method returns key path ! Problem is an error Cannot convert type 'char' to 'string inforeach (string key_value in key.GetValue("NetCfgInstanceId").ToString()) .
Full code is

    public string key_path(RegistryKey root, string root_path, string search_key)
        {
            string path = string.Empty;
            foreach (string keyname in root.GetSubKeyNames())
            {
                try
                {
                    using (RegistryKey key = root.OpenSubKey(keyname, true))
                    {

                        foreach (string key_value in key.GetValue("NetCfgInstanceId").ToString()) 
                        {
                            if (key_value == search_key)
                            {
                                string reg_path = (string)key.GetValue("NetCfgInstanceId");
                                path = reg_path;
                            }
                            else
                            {
                                path = "Can't find key !";
                            }
                        }
                    }
                }
                catch (System.Security.SecurityException)
                {
                    //Do nothing !!!
                }
            }
            return path;
        }

        private void kryptonButton4_Click(object sender, EventArgs e)
        {
            var answer = key_path(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}", "{C80949A4-CEDA-4F29-BFE2-059856D7F745}");
            MessageBox.Show(answer);
        } 

How can solve this problem ?
Cheers and thank's for advance !

Hi again,
the code

foreach (string key_value in key.GetValue("NetCfgInstanceId").ToString())

actually doesn't have any sense. What you want to do is to get value of NetCfgInstanceId in every subkey. key.GetValue(NetCfgInstanceId)returns a single value (string), there's nothing to enumerate. Compiler doesn't allow you to do this, because string can be divided only into chars, string doesn't contain any strings within ;)

So you can skip your foreach loop and compare this value with your search key directly:
key.GetValue("NetCfgInstanceId").ToString() == search_key

And you can use break or return statement when your searching is finished as there is no reason to continue searching when the value has been already found ;)

EDIT:

I've checked your code and it won't work. Here you have a correct version returning full path:

public string key_path(RegistryKey root, string root_path, string search_key)
        {
            string path = string.Empty;
            foreach (string keyname in root.OpenSubKey(root_path).GetSubKeyNames()) //for every subkey in the path...
            {
                try
                {
                    using (RegistryKey key = root.OpenSubKey(root_path).OpenSubKey(keyname, true)) //opens subkeys 0000, 0001...
                    {
                        if (key.GetValue("NetCfgInstanceId").ToString() == search_key) //compares NetCfgInstanceId with your search string
                        {
                            return root+"\\"+root_path+"\\"+keyname; //if found, returns a result
                        }
                    }
                }
                catch (System.Security.SecurityException)
                {
                    //Do nothing !!!
                }
            }
            return "not found!";
        }

In your code, you're using all the time root.GetSubKeyNames() which gets subkeys' names of root (HKEY_LOCAL_MACHINE), it doesn't do anything with the path.

What is more, to access the registry, you will have add app.manifest file. You can do it by adding new item to your project (choose "Application Manifest File"). It should look like that:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
         <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
         </requestedPrivileges>
      </security>
   </trustInfo>
</asmv1:assembly>
commented: Woow...Thanks for help and best explanation ! +0

AWesome !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.