string user = @"NT AUTHORITY\NETWORK SERVICE";
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\eventlog");
RegistrySecurity regSec = new RegistrySecurity();
RegistryAccessRule rule = new RegistryAccessRule(
user,
RegistryRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow);
regSec.AddAccessRule(rule);
key.SetAccessControl(regSec);
I’m getting System.Unauthorizedaccessexception: “Cannot write to the registry key.” at key.SetAccessControl(regSec);
I’m running the app as admin.
I think Network Service account, by default, does not have write access to the registry.
See http://msdn.microsoft.com/en-us/library/ff647402.aspx#paght000015_registryaccess
The exception probably has nothing to with whether the app was run as admin.