33

I get a null back from this attempt to access the Windows Registry:

using (RegistryKey registry = Registry.LocalMachine.OpenSubKey(keyPath))

keyPath is SOFTWARE\\TestKey

The key is in the registry, so why is it not finding it under the Local Machine hive?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
  • 1
    Are you properly escaping KeyPath? Is it "SOFTWARE\\TestKey", and not "SOFTWARE\TestKey"? – Michael Aug 12 '09 at 21:05
  • yes, I posted it wrong. It is actually setup to use \\ – PositiveGuy Aug 12 '09 at 21:09
  • 1
    So this has worked on my boss's PC just fine. No reason why it should not here. I gave asp.net account access to it since I'm running the VS web server and still returns null. – PositiveGuy Aug 12 '09 at 21:11

3 Answers3

66

It can happen if you are on a 64-bit machine. Create a helper class first (requires .NET 4.0 or later):

public class RegistryHelpers
{

    public static RegistryKey GetRegistryKey()
    {
        return GetRegistryKey(null);
    }

    public static RegistryKey GetRegistryKey(string keyPath)
    {
        RegistryKey localMachineRegistry
            = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                      Environment.Is64BitOperatingSystem
                                          ? RegistryView.Registry64
                                          : RegistryView.Registry32);

        return string.IsNullOrEmpty(keyPath)
            ? localMachineRegistry
            : localMachineRegistry.OpenSubKey(keyPath);
    }

    public static object GetRegistryValue(string keyPath, string keyName)
    {
        RegistryKey registry = GetRegistryKey(keyPath);
        return registry.GetValue(keyName);
    }
}

Usage:

string keyPath = @"SOFTWARE\MyApp\Settings";
string keyName = "MyAppConnectionStringKey";

object connectionString = RegistryHelpers.GetRegistryValue(keyPath, keyName);

Console.WriteLine(connectionString);
Console.ReadLine();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raghav
  • 8,772
  • 6
  • 82
  • 106
2

In your comment to Dana you said you gave the ASP.NET account access. However, did you verify that that is the account that the site in running under? Impersonate and the anonymous access user can be easy to overlook.

UNTESTED CODE:

Response.Clear();  
Response.Write(Environment.UserDomainName + "\\" + Environment.UserName);  
Response.End();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KellCOMnet
  • 1,859
  • 3
  • 16
  • 27
  • How can I figure out which account the site is running under without having to write code to expose? – PositiveGuy Aug 12 '09 at 21:29
  • well the above is generally how i troubleshoot account issues although i do add a check in that it is coming from my ip address so that not just everyone can see it. – KellCOMnet Aug 14 '09 at 12:19
1

Just needed to change it from

using (RegistryKey registry = Registry.LocalMachine.OpenSubKey(keyPath))

to

using (RegistryKey registry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default).OpenSubKey(keyPath))

(Use RegistryKey instead of Registry , add the RegistryView, and put the hive-Local Machine as a method parameter.)

vapcguy
  • 7,097
  • 1
  • 56
  • 52
  • 1
    RegistryView.Default didn't work for me, I used what was specified in the most popular answer (but I like this answer's conciseness): Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32 – Ronny D'Hoore Oct 13 '22 at 12:17
  • Yeah, I think when you're going between 32-bit & 64-bit it makes a difference, so you have to specify which one. So adding that in, in place of `RegistryView.Default`, is a good idea. – vapcguy Oct 21 '22 at 18:11