Link to home
Start Free TrialLog in
Avatar of dustock
dustockFlag for United States of America

asked on

RegistryKey is null after being assigned

I have the code below to loop through the registry to get the the subkeys within the PNP0501 key.  When I create RegistryKey comKeys everything works fine and I loop through the registry to populate my list (lstSubKeys).  When I go into my foreach loop to assign COMSubKey a value based on the items in the list, it always is null.  I have tried removing the string.format and just putting in + strSubKey +, I have tried assigning the subkey to a string strComPath and putting that in the COMSubKey.OpenSubkey.  I have also tried closing and disposing of comKeys before creating the RegistryKey COMSubKey, nothing seems to work.  Any body have any idea why it wont assign the value to COMSubKey?


        public void getCOMPortKeys()
        {
            List<String> lstSubKeys = new List<String>();
            List<String> lstPortName = new List<String>();
            RegistryKey comKeys = Registry.LocalMachine;
            RegistryKey COMSubKey = Registry.LocalMachine;

            comKeys = comKeys.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\ACPI\PNP0501");

            lstSubKeys = loopRegistry(comKeys);           

            foreach (String strSubKey in lstSubKeys)
            {
                COMSubKey = COMSubKey.OpenSubKey(String.Format(@"SYSTEM\CurrentControlSet\Enum\ACPI\PNP0501\{0}\Device Parameters\PortName", strSubKey));
                
                lstPortName = loopRegistry(COMSubKey);
            }
        }

Open in new window

Avatar of Ess Kay
Ess Kay
Flag of United States of America image

check if the Device Parameters and PortName folders exist

what errors are you getting when you debug it
Avatar of dustock

ASKER

Those folders exist, I get no errors.  I tried wrapping it in a try catch block to see if I could get an error and it went through the try but I still had a null value.  When I create RegistryKey COMSubKey = Registry.LocalMachine; its value is "HKEY_LOCAL_MACHINE" but once I enter the foreach loop and try to open the subkey, its null.  I even copied the key location to verify my spelling.
also, i stubled upon this. Its the difference between chip archetecture.


https://stackoverflow.com/questions/13728491/opensubkey-returns-null-for-a-registry-key-that-i-can-see-in-regedit-exe
A 32-bit application on a 64-bit OS will be looking at the HKLM\Software\Wow6432Node node by default. To read the 64-bit version of the key, you'll need to specify the RegistryView:

using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
{
   // key now points to the 64-bit key
}
The API to do this was added in .NET 4.0; if you're still using 3.5, you'll need to use P/Invoke to access the 64-bit keys: http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/
Avatar of dustock

ASKER

I do have a 64bit OS and I am making a 32 bit application.  But this is talking about accessing the SOFTWARE key, which I am access SYSTEM.  And I can successfully reach "SYSTEM\CurrentControlSet\Enum\ACPI\PNP0501" without any issues.  Its when I use my loop of keys under PNP0501  that my assignments are null.  I will take a look at this and see if it helps, but it doesn't seem related to my problem since I am accessing the SYSTEM key and there is no WOW64 folder to worry about there.
ASKER CERTIFIED SOLUTION
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dustock

ASKER

I'll give that a shot this afternoon and see if it helps
Avatar of dustock

ASKER

Thanks esskayb2d, while the RegistyView.Registy64 wasn't the solution your if statement was.  I was looking for the value of PortName and replacing the text in your if statement with my path and key in the getvalue solved my issue.  I feel dumb now, but your help was much appreciated!
glad i helped