Solved

RegistryKey is null after being assigned

Posted on 2014-04-24
9
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40020768
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
ID: 40020893
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
ID: 40021016
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:Ess Kay
ID: 40021034
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
 
LVL 9

Author Comment

by:dustock
ID: 40021086
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
ID: 40021239
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
ID: 40022880
I'll give that a shot this afternoon and see if it helps
0
 
LVL 9

Author Closing Comment

by:dustock
ID: 40022904
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
ID: 40023470
glad i helped
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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