0

The following registry path clearly exists at my machine but I get a null pointer exception:

  var myKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Android Studio", false);
  var value = (String)myKey.GetValue("JdkPath"); // myKey is null 
  if (!String.IsNullOrEmpty(value)) {
    //...
  }

Why?

  • What is the full path to the key? Is it `HKEY_LOCAL_MACHINE\SOFTWARE\Android Studio`? if you compile your program to target x86, your program will be looking for `HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Android Studio`. – kennyzx Feb 27 '16 at 06:37
  • @kennyzx, it's the full path. –  Feb 27 '16 at 06:40
  • Write a new registry key into the HKLM hive under the Software key and make sure it then exists where you expect it to be. – khargoosh Feb 27 '16 at 07:15
  • http://stackoverflow.com/questions/13728491/opensubkey-returns-null-for-a-registry-key-that-i-can-see-in-regedit-exe – romanoza Feb 27 '16 at 14:48

1 Answers1

0

I don't have Android studio, so I tried with 7-Zip. It is a good idea to check the version 32/64 bit of the software with RegistryView enumeration:

string path = @"SOFTWARE\7-Zip";

RegistryKey keys32 = RegistryKey.OpenBaseKey ( RegistryHive.LocalMachine, RegistryView.Registry32 );

RegistryKey rkPath = null;
rkPath = keys32.OpenSubKey ( path );

if ( rkPath == null )
{
    Console.WriteLine ("32 bit version is null. Let's try 64 bit version");
    RegistryKey keys64 = RegistryKey.OpenBaseKey ( RegistryHive.LocalMachine, RegistryView.Registry64 );
    rkPath = keys64.OpenSubKey ( path );
}
string result = rkPath.GetValue ( "Path" ).ToString ( );
Bakudan
  • 19,134
  • 9
  • 53
  • 73