[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

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

0
dustock
Asked:
dustock
  • 5
  • 4
1 Solution
 
Ess KayEntrapenuerCommented:
check if the Device Parameters and PortName folders exist

what errors are you getting when you debug it
0
 
dustockAuthor Commented:
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
 
Ess KayEntrapenuerCommented:
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
Ess KayEntrapenuerCommented:
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
 
dustockAuthor Commented:
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
 
Ess KayEntrapenuerCommented:
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
 
dustockAuthor Commented:
I'll give that a shot this afternoon and see if it helps
0
 
dustockAuthor Commented:
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
 
Ess KayEntrapenuerCommented:
glad i helped
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now