Registry.LocalMachine.OpenSubKey(...) is returning NULL

1,282 views
Skip to first unread message

thephatp

unread,
Mar 27, 2008, 10:26:19 PM3/27/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
This is incredibly strange. I used regedit and went to
HKEY_LOCAL_MACHINE and create a new subkey under "Software". I added
a few more, then went into code to try to read them. I kept getting
null returned when I tried to open them, so I decided to try this:

Registry.LocalMachine.GetSubKeyNames()

The results where dumbfounding--they did NOT match what I see in the
Registry under HKEY_LOCAL_MACHINE. The list values returned from the
Registry.LocalMachine.GetSubKeyNames() method is MUCH longer than the
list of subkeys that exist in the registry.

What gives? (I'm really hoping this is an easy explanation for
someone, but I'm just not getting it.)

Also, I'm having another, just as concerning problem. In both the
Registry and in code from the list returned by GetSubKeyNames, the
subkey "Microsoft" exists. However, notice the following:

RegistryKey key1 = Registry.LocalMachine.OpenSubKey( "Software" ); //
This returns the key -- Success
RegistryKey key2 = Registry.LocalMachine.OpenSubKey( "Software\
\Microsoft" ); // This returns NULL :(
RegistryKey key3 = key1.OpenSubKey( "Microsoft" ) ; // This also
returns NULL :(

Why could this be happening?

Thanks in advance!

Chad

Cerebrus

unread,
Mar 28, 2008, 1:56:25 AM3/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Strange indeed ! I may be wrong, but I think that if there is a
SecurityException while accesing the key, Nothing/Null would be
returned to you. Some of the keys in the HKLM node are not accessible
to normal users. Maybe you could try your experiment with the HKCU key
and let us know what happens.

Andrew Badera

unread,
Mar 28, 2008, 5:56:51 AM3/28/08
to DotNetDe...@googlegroups.com
I think it would throw a SecurityException not return a null, if the key were inaccessible ...
--
--Andy Badera
http://andrew.badera.us/
and...@badera.us
(518) 641-1280
Google me: http://www.google.com/search?q=andrew+badera

thephatp

unread,
Mar 28, 2008, 8:02:41 AM3/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
The issue that is the most troubling is that the list returned in code
(via GetSubKeyNames() method) is different than the list in the actual
registry.

Steve made a good point here:

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/65a7a284f2aeff70/8bfed59b5dbfe3f3?hl=en#8bfed59b5dbfe3f3

Quoting:
"Vista, by chance?

If so, you may want to look up Registry
Virtualization...http://msdn2.microsoft.com/en-us/library/
aa965884.aspx "
> --Andy Baderahttp://andrew.badera.us/
> and...@badera.us
> (518) 641-1280
> Google me:http://www.google.com/search?q=andrew+badera- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages