Solved

RegistryKey is null after being assigned

Posted on 2014-04-24
9
242 Views
Last Modified: 2014-04-25
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

0
Comment
Question by:dustock
  • 5
  • 4
9 Comments
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
check if the Device Parameters and PortName folders exist

what errors are you getting when you debug it
0
 
LVL 9

Author Comment

by:dustock
Comment Utility
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.
0
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
0
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
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/
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 9

Author Comment

by:dustock
Comment Utility
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.
0
 
LVL 15

Accepted Solution

by:
Ess Kay earned 500 total points
Comment Utility
perhaps, but there is a command there that may be useful, which points to RegistryView.Registry64


If, that fixes the issue, make sure to run a check in what system you are using, if client is 32, then do not try to access the 64 directory.

easy way for that is


If Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\0").GetValue("Identifier").ToString().Contains("x86") Then //"32"
{
   ...32 CODE..
}
ELSE
{
  ...64 code..
}
0
 
LVL 9

Author Comment

by:dustock
Comment Utility
I'll give that a shot this afternoon and see if it helps
0
 
LVL 9

Author Closing Comment

by:dustock
Comment Utility
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!
0
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
glad i helped
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now