title: How to use RegistryKey.SetValue
body: <p>I'm trying to create a new registry key using following code and getting this error:</p>
  
  <pre><code>Cannot write to the registry key.
  </code></pre>
  
  <p>Where am I going wrong???</p>
  
  <pre><code>var rs = new RegistrySecurity();
  string user = Environment.UserDomainName + "\\" + Environment.UserName;
  rs.AddAccessRule(new RegistryAccessRule(user,
                                          RegistryRights.WriteKey | RegistryRights.SetValue,
                                          InheritanceFlags.None,
                                          PropagationFlags.None,
                                          AccessControlType.Allow));
  RegistryKey key;
  key = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", RegistryKeyPermissionCheck.ReadSubTree, rs);
  key.SetValue("kashif", 1, RegistryValueKind.DWord);
  key.Close();
  </code></pre>