?
Solved

RegistryKey is null after being assigned

Posted on 2014-04-24
9
Medium Priority
?
259 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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