本文介绍了通过C#从注册表中的"UserChoice"项中删除“拒绝"规则(权限)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究文件关联.我确定在其中有一个名为UserChoice的钥匙:

I am working on File Associations. I have identified that there is a key called UserChoice in:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\[ext].

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

I have been able to read from and write to the UserChoice key provided that I create it and that it has not already been created by Windows. However, if the UserChoice key has already been created by Windows, then I need to run as Administrator to get access to the key. My ultimate goal is to delete the UserChoice key.

我注意到Windows在UserChoice键上设置了 Deny 规则,这阻止了我删除该键.如果我可以成功删除该规则,则可以删除UserChoice键.这是我尝试过的代码:

I have noted that Windows places a Deny rule on the UserChoice key which is preventing me from deleting that key. If I can succeed in removing that rule, I believe that I'll be able to delete the UserChoice key. Here is the code that I have tried:

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密钥时,它将为 Deny类型的当前用户添加一个安全规则.权限:特殊.此规则不是继承的,仅适用于UserChoice键.

When Windows creates the UserChoice key it adds a security rule for the current user of Type Deny; permission: Special. This rule is not inherited and applies to the UserChoice key only.

随着一些混乱并以管理员身份运行,我可以访问该RegistryAccessRule.但是,即使以管理员身份运行,我也无法删除此规则.我在研究中读到某处没有编程方式可以做到这一点.我可以通过RegEdit删除此规则.我还可以使用NirSoft的文件类型管理器删除UserChoice键.所以我假设有一些方法可以做到这一点.

With some messing about and running as Administrator I am able to access that RegistryAccessRule. However even running as Administrator, I cannot remove this rule. I have read somewhere in my research that there is not a programmatic way to do it. I can remove this rule via RegEdit. I can also remove the UserChoice key using File Types Manager from NirSoft. So I assume there is some way to do this.

摘要:是否可以删除拒绝"规则,以便删除UserChoice键?

Summary: Is there a way that I can remove the Deny rule so that I can delete the UserChoice key?

推荐答案

您的代码示例和 answer @ali撰写的文章为我提供了一种解决方案,该方案可克服Windows放置在UserChoice密钥上的安全设置,从而使我能够删除该密钥.

Your code example and the revisions suggested in the answer by @ali lead me to a solution for overcoming the security setting that Windows places on the UserChoice key which enabled me to delete that key.

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

My solution presumes that the UserChoice key is present in the HKEY_CURRENT_USER (HKCU) hive. If that is the case, the user owns the UserChoice key and therefore has the necessary privileges to change the security settings on that key and ultimately delete it. (This means that the user does not need to be a member of the Administrators group.)

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

The extensionKey parameter of this method is the parent key of the UserChoice key.

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, 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);
}

这篇关于通过C#从注册表中的"UserChoice"项中删除“拒绝"规则(权限)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 15:58