我正在开发一个C#应用程序,我正在尝试为注册表的所有本地用户设置授权。
有我的代码:
RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);
RegistryKey vkey = key.OpenSubKey("Software\\MyApplication", true);
if (vkey == null)
{
session.Log("Software\\MyApplication" + " is null : create it");
vkey = key.CreateSubKey("Software\\MyApplication", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
RegistrySecurity rs = new RegistrySecurity();
rs = vkey.GetAccessControl();
SecurityIdentifier secu = new SecurityIdentifier("S-1-2-0");
NTAccount name = secu.Translate(typeof(NTAccount)) as NTAccount;
rs.AddAccessRule(new RegistryAccessRule(name.ToString(), RegistryRights.WriteKey | RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.FullControl, AccessControlType.Allow));
之后,注册表被正确创建,但授权错误。
我想设置授权以选中此框(ContrôleTotal=完全控制):
修改:
我做了一个测试:
AuthorizationRuleCollection
AuthorizationRuleCollection
在那里,我发现为“S-1-5-32-545”添加了一个新规则,其中访问掩码为0x000F003F。在那里,我以编程方式进行更改。有我的代码:
RegistrySecurity rs = new RegistrySecurity();
rs = vkey.GetAccessControl();
SecurityIdentifier secu = new SecurityIdentifier("S-1-5-32-545");
NTAccount name = secu.Translate(typeof(NTAccount)) as NTAccount;
rs.AddAccessRule(new RegistryAccessRule(name.ToString(), RegistryRights.WriteKey | RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.FullControl, AccessControlType.Allow));
当我得到AuthorizationRuleCollection
时,我看到添加了新规则。但是在程序结束后,注册表中没有任何变化。
如何验证/清除我的修改?