0

I need to check the availability and also read some registry key by CLR(C#), registry keys already written by another application. As a sample:

 public bool IsKeyAvailable(string KeyID)
 {
     string keyToRead = @"Software\myRoot\myApp\" + KeyID;

     using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(keyToRead, RegistryKeyPermissionCheck.ReadSubTree))
     {
        if (regKey == null)
           return false;
        return true;
     }
 }

Checking & reading code are working fine outside of the CLR, but within the CLR the same code doesn't working, Already signed the CLR and assembly created WITH PERMISSION_SET = UNSAFE.

What could be missed for this scenario to find and read my registry keys by CLR?

1 Answers1

0

Use this code :

Registry.LocalMachine.OpenSubKey("SOFTWARE", true);

RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\yourapp\yourkey");

string value = "";

if (masterKey != null) { value = masterKey.GetValue("yourvalue").ToString(); } masterKey.Close();

Kumar Manish
  • 3,746
  • 3
  • 37
  • 42
  • Kumar My key is(and has to be) stored under the HKEY_CURRENT_USER not localMachine, however your code is not working as well – user1455210 May 05 '14 at 01:55
  • Try this code :string InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\AppPath", "Installed", null); if (InstallPath != null) { // Do stuff } – Kumar Manish May 05 '14 at 03:32