Registry.GetValue always return null

匿名 (未验证) 提交于 2019-12-03 01:46:01

问题:

I have the following key in my registry:

under:HKEY_LOCAL_MACHINE\SOFTWARE\RSA I have value object call - WebExControlManagerPath and its value is c:\

I am trying to do this:

var r = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RSA", "WebExControlManagerPth",null);  if(r!=null)     ProcessAsUser.Launch(ToString()); 

But r value is always null.

Any ideas?

回答1:

You don't access the HKEY_LOCAL_MACHINE hive the same way you do in C# as you would in batch scripting. You call Registry.LocalMachine, as such:

        RegistryKey myKey = Registry.LocalMachine.OpenSubKey( @"Software\RSA", false);         String value = (String)myKey.GetValue("WebExControlManagerPth");          if (!String.IsNullOrEmpty(value))         {             ProcessAsUser.Launch(ToString());         } 

Update:

If it returns null, set your build architecture to Any CPU. The operating system may virtualize 32-bit and 64-bit registries differently. See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa965884%28v=vs.85%29.aspx, Reading 64bit Registry from a 32bit application, and http://msdn.microsoft.com/en-us/library/windows/desktop/ms724072%28v=vs.85%29.aspx.



回答2:

The statement of Jason is right, the operating system is the problem, the below code will help you to resolve.

RegistryKey localKey; if(Environment.Is64BitOperatingSystem)     localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); else     localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);  string value = localKey.OpenSubKey("RSA").GetValue("WebExControlManagerPth").ToString(); 


回答3:

if you are using 64 bit operating system, when you are trying to get HKEY_LOCAL_MACHINE\SOFTWARE\RSA it is actually looking for HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\RSA that is why you get null



回答4:

look at the security permissions on the registry key with regedt32.exe; check if you are running as admin and have UAC turned off. According to the opensubkey documentation it needs to be opened first before accessing any keys; http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx



回答5:

I had extra "\" in the beginning of my path, make sure that is set right.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!