2

我想更改所有权,然后更改注册表项的权限。

这是我到目前为止的代码:

        var id = WindowsIdentity.GetCurrent();

        if (!Win32.SetPrivilege(Win32.TakeOwnership, true))
            throw new Exception();

        if (!Win32.SetPrivilege(Win32.Restore, true))
            throw new Exception();

        var hklm = RegistryKey.OpenBaseKey(registryHive, is64Key ? RegistryView.Registry64 : RegistryView.Registry32);
        using (RegKey = hklm.OpenSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.TakeOwnership))
        {
            if (RegKey == null)
                throw new Exception("clé de registre non trouvée");

            _security = RegKey.GetAccessControl(AccessControlSections.All);

            var oldId = _security.GetOwner(typeof (SecurityIdentifier));
            _oldSi = new SecurityIdentifier(oldId.ToString());

            _security.SetOwner(id.User);
            RegKey.SetAccessControl(_security);
        }

        using (RegKey = hklm.OpenSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions))
        {
            _fullAccess = new RegistryAccessRule(id.User, RegistryRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
            _security.AddAccessRule(_fullAccess);
            RegKey.SetAccessControl(_security);
        }

一切正常,但在 regedit 中,子项权限仅包含我的用户,所有其他用户都被删除。

前 :

更改权限前

后 :

更改权限后

似乎继承的权利被删除了。

我快要成功了,它必须错过一个参数,但我看不到哪个参数。

4

1 回答 1

3

尝试添加这个:

_security.SetAccessRuleProtection(false, false);

在你调用这个之前:

RegKey.SetAccessControl(_security);

这样做将确保禁用“防止继承”(即允许继承)。

于 2015-04-05T00:39:35.897 回答