注册表项存在,但我收到空指针异常

本文关键字:空指针异常 存在 注册表 | 更新日期: 2023-09-27 18:36:24

以下

注册表路径显然存在于我的机器上,但我收到空指针异常:

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

为什么?

注册表项存在,但我收到空指针异常

我没有Android studio,所以我尝试使用7-Zip。最好使用 32/64 位检查软件的版本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 ( );