按代码修改注册表授权

时间:2016-12-01 13:41:00

标签: c#

我正在开发一个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));

之后,注册表被正确创建,但授权错误。

enter image description here

我想设置授权以选中此框(ContrôleTotal=完全控制):

enter image description here

修改

我做了一个测试:

  • 获取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时,我看到添加了新规则。但是在程序结束后,注册表中没有任何变化。

如何验证/清除我的修改?

0 个答案:

没有答案
相关问题