加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

通过C#从注册表中的“UserChoice”键中删除“拒绝”规则(权限)

发布时间:2020-12-15 17:17:00 所属栏目:百科 来源:网络整理
导读:我正在研究文件关联.我已经确定在UserChoice中有一个名为UserChoice的键: HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts[ext]. 我已经能够读取和写入UserChoice密钥,前提是我创建它并且它尚未由Windows创建.但是,如果Wi
我正在研究文件关联.我已经确定在UserChoice中有一个名为UserChoice的键:

HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts[ext].

我已经能够读取和写入UserChoice密钥,前提是我创建它并且它尚未由Windows创建.但是,如果Windows已经创建了UserChoice密钥,那么我需要以管理员身份运行才能访问密钥.我的最终目标是删除UserChoice键.

我注意到Windows在UserChoice键上放置了拒绝规则,这阻止我删除该键.如果我能成功删除该规则,我相信我将能够删除UserChoice密钥.这是我尝试过的代码:

public static void ShowSecurity(RegistryKey regKeyRoot,string user) {
    RegistrySecurity security = regKeyRoot.GetAccessControl(AccessControlSections.All);

    foreach (RegistryAccessRule ar in
        security.GetAccessRules(true,true,typeof(NTAccount))) {

        if (ar.IdentityReference.Value.Contains(User) &&
                ar.AccessControlType.ToString().ToLower() == "deny") {

            security.RemoveAccessRuleSpecific(ar);
            regKeyRoot.SetAccessControl(security);
        }
    }
}

当Windows创建UserChoice键时,它为Type Deny的当前用户添加安全规则;许可:特别.此规则不是继承的,仅适用于UserChoice键.

随着管理员的一些混乱和运行,我能够访问RegistryAccessRule.但是,即使以管理员身份运行,我也无法删除此规则.我在研究中的某处读到,没有一种程序化的方法可以做到这一点.我可以通过RegEdit删除此规则.我还可以使用NirSoft的文件类型管理器删除UserChoice密钥.所以我认为有一些方法可以做到这一点.

简介:有没有办法可以删除Deny规则,以便删除UserChoice密钥?

解决方法

您的代码示例以及@ali在 answer中建议的修订引导我找到一个解决方案,用于克服Windows在UserChoice键上放置的安全设置,这使我能够删除该密钥.

我的解决方案假设UserChoice密钥存在于HKEY_CURRENT_USER(HKCU)配置单元中.如果是这种情况,则用户拥有UserChoice密钥,因此具有更改该密钥的安全设置并最终将其删除的必要权限. (这意味着用户不需要是Administrators组的成员.)

此方法的extensionKey参数是UserChoice键的父键.

static void DeleteUserChoiceKey(RegistryKey extensionKey)
{
    const string userChoiceKeyName = "UserChoice";

    using (RegistryKey userChoiceKey =
        extensionKey.OpenSubKey(userChoiceKeyName,RegistryKeyPermissionCheck.ReadWriteSubTree,RegistryRights.ChangePermissions))
    {
        if (userChoiceKey == null) { return; }
        string userName = WindowsIdentity.GetCurrent().Name;
        RegistrySecurity security = userChoiceKey.GetAccessControl();

        AuthorizationRuleCollection accRules =
            security.GetAccessRules(true,typeof(NTAccount));

        foreach (RegistryAccessRule ar in accRules)
        {
            if (ar.IdentityReference.Value == userName &&
                ar.AccessControlType == AccessControlType.Deny)
            {
                security.RemoveAccessRuleSpecific(ar); // remove the 'Deny' permission
            }
        }

        userChoiceKey.SetAccessControl(security); // restore all original permissions
                                                  // *except* for the 'Deny' permission
    }

    extensionKey.DeleteSubKeyTree(userChoiceKeyName,true);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读