我正在Windows服务中修改注册表权限,基本上我想做的是
这是我的代码,请您查看并告知我,如果我需要更改任何内容,我编写了一个示例应用程序并手动检查了注册表权限。然后,下次我运行我的应用程序时,它没有访问注册表项的权限,甚至没有我赋予已验证用户的读取权限。
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MyCompany\Somekey",RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistryAccessRule rule1 = new RegistryAccessRule(
new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null),
RegistryRights.ReadKey,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
RegistryAccessRule rule2 = new RegistryAccessRule(
new SecurityIdentifier(WellKnownSidType.WorldSid, null),
RegistryRights.WriteKey,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Deny);
RegistryAccessRule rule3 = new RegistryAccessRule(
new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null),
RegistryRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
RegistryAccessRule rule4 = new RegistryAccessRule(
new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null),
RegistryRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
RegistrySecurity registrySecurity = regKey.GetAccessControl();
registrySecurity.AddAccessRule(rule1);
registrySecurity.AddAccessRule(rule2);
registrySecurity.AddAccessRule(rule3);
registrySecurity.AddAccessRule(rule4);
regKey.SetAccessControl(registrySecurity);