title: Unable to set registry values using RegistryKey class in C# .NET
body: <p>I am using Visual C# 2010 and am having problems setting registry keys. I assumed this was to do with the fact that I wasn't running it as admin at first but i have tried building the Release and then right clicking the exe and selecting 'run as administrator' to no avail.</p>
  
  <p>I also tried using the RegistryPermission class which didn't seem to make any difference.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>RegistryKey rkey = Registry.LocalMachine;
  // RegistryPermission f = new RegistryPermission(
  //    RegistryPermissionAccess.Write | RegistryPermissionAccess.Read,
   //   @"HKEY_LOCAL_MACHINE\SOFTWARE\Company\Product");
  
  
  /**********************/
  /* set registry keys  */
  /**********************/
  RegistryKey wtaKey = rkey.OpenSubKey(@"SOFTWARE\Company\Product", true);
  try
  {
      wtaKey.SetValue("key1", 123);
      wtaKey.SetValue("key2", 567);
      wtaKey.SetValue("key3", textbox.Text);
      wtaKey.SetValue("key4", "some string");
  }
  catch (UnauthorizedAccessException ex)
  {
      MessageBox.Show(ex.Message);
      return;
  }
  </code></pre>
  
  <p>This gives me the error message from the exception each time I run it, even with 'run as administrator'. Any ideas how I can get around this? It seems strange because my standard user account allows me to go into regedit and manually change these values no problem.</p>