6

I'm trying to create a new registry key using following code and getting this error:

Cannot write to the registry key.

Where am I going wrong???

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();
Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
kashif
  • 3,713
  • 8
  • 32
  • 47

2 Answers2

8

You need to open the newly created key for read/write access:

key = Registry.LocalMachine.CreateSubKey(
    @"Software\Microsoft\Windows\CurrentVersion\Policies\System",
    RegistryKeyPermissionCheck.ReadWriteSubTree, // read-write access
    rs);
Jon
  • 428,835
  • 81
  • 738
  • 806
  • @kashif: Is the application running with elevated privileges? – Jon Jan 25 '13 at 22:19
  • I'm logged in with administrator privileges. when I'm building the application and right clicking the exe and then running it as administrator it runs fine that i don't want. that's why I am using registry security in it. – kashif Jan 25 '13 at 22:23
  • @kashif: It doesn't work like that. You are setting the security for the new key, but to create the new key you need to have write permission for `System` first. And the only way to get that is to run elevated. – Jon Jan 25 '13 at 22:26
  • 4
    @kashif: You cannot get administrative privileges "through coding". If you could, everyone else would do that as well and the UAC elevation prompt would have no reason to exist since everyone would be bypassing it. – Jon Jan 25 '13 at 22:29
  • thanks. you solved my problem but having altered my coding the way you suggested created another problem that i solved by this link http://stackoverflow.com/questions/3446211 – kashif Jan 25 '13 at 23:02
1

You don't need the rs part unless you are trying to assign specific permissions to the key you are creating.

You need to:

  1. Open the key where you want to create your subkey and assign it to a RegistryKey variable. Add a true Boolean to mark it as writeable.
  2. Create your subkey using that variable, and assign that back to the same variable.
  3. Set the value using your RegistryKey variable.

Example:

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", true);
key = key.CreateSubKey("MyNewKey");
key.SetValue("kashif", 1, RegistryValueKind.DWord);

If you just want to edit an existing key, as System is, you don't use CreateSubKey, because the key already exists. That is only for creating new keys. You use OpenSubKey.

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", true);
key.SetValue("kashif", 1, RegistryValueKind.DWord);
vapcguy
  • 7,097
  • 1
  • 56
  • 52