I am granting permission for NETWORK SERVICE to access a registry key that I need to access via a console application which I run as NETWORK SERVICE. Here is how I create the key and Grant permission.
Microsoft.Win32.RegistryKey key; key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE", RegistryKeyPermissionCheck.ReadWriteSubTree); RegistrySecurity rs = new RegistrySecurity(); rs = key.GetAccessControl(); rs.AddAccessRule(new RegistryAccessRule("NETWORK SERVICE", RegistryRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)); key.SetAccessControl(rs); key = key.CreateSubKey("RM", RegistryKeyPermissionCheck.ReadWriteSubTree); key = key.CreateSubKey("CSVExtraction", RegistryKeyPermissionCheck.ReadWriteSubTree); key.SetValue("Failure Tally", "0"); But when I try to setValue to the key using the following code I am getting error. The code that I use to set value is given below.
Microsoft.Win32.RegistryKey key; key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree); key = key.CreateSubKey("RM", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree); key = key.CreateSubKey("CSVExtraction", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree); key.SetValue("Failure Tally, "1"); But Here I am getting an error saying access to HKLM/LOCAL MACHINE/SOFTWARE denied. But I have added access rule using the above code. But when I go and check the access manually the access is added to the WOW6432node.