C# 正在获取注册表项删除权限

C# 正在获取注册表项删除权限,c#,windows,permissions,registry,C#,Windows,Permissions,Registry,我试图让我的程序确定它是否有权删除C#中的注册表项。我一直在尝试这段代码,它返回true,而事实上我对注册表项没有正确的权限 public static bool CanDeleteKey(RegistryKey key) { try { if (key.SubKeyCount > 0) { bool ret = false; foreach (string subKey in key.Get

我试图让我的程序确定它是否有权删除C#中的注册表项。我一直在尝试这段代码,它返回true,而事实上我对注册表项没有正确的权限

public static bool CanDeleteKey(RegistryKey key)
{
    try
    {
        if (key.SubKeyCount > 0)
        {
            bool ret = false;

            foreach (string subKey in key.GetSubKeyNames())
            {
                ret = CanDeleteKey(key.OpenSubKey(subKey));

                if (!ret)
                    break;
            }

            return ret;
        }
        else
        {
            RegistryPermission r = new 
          RegistryPermission(RegistryPermissionAccess.AllAccess, key.ToString());
            r.Demand();
            return true;
        }

    }
    catch (SecurityException)
    {
        return false;
    }
}
我传递给此函数的注册表项是
HKEY\U CLASSES\U ROOT\eHomeSchedulerService.TVThumbnailCache
。它应该重新诅咒子键
CLSID
,并返回false,因为完全控制权限仅为TrustedInstaller设置

以下是regedit对HKEY\U CLASSES\u ROOT\eHomeSchedulerService.TVThumbnailCache\CLSID的权限:


我应该注意,我是以管理权限运行代码的。另外,我知道我可以在删除注册表项时使用try-catch块,但我想知道是否可以事先删除它。

拥有读取权限允许您打开注册表项。尝试使用:

key.DeleteSubKey(subkey)

我能够在谷歌上做一些挖掘,我想出了这样一个代码,它似乎做到了这一点:

    /// Checks if we have permission to delete a registry key
    /// </summary>
    /// <param name="key">Registry key</param>
    /// <returns>True if we can delete it</returns>
    public static bool CanDeleteKey(RegistryKey key)
    {
        try
        {
            if (key.SubKeyCount > 0)
            {
                bool ret = false;

                foreach (string subKey in key.GetSubKeyNames())
                {
                    ret = CanDeleteKey(key.OpenSubKey(subKey));

                    if (!ret)
                        break;
                }

                return ret;
            }
            else
            {
                System.Security.AccessControl.RegistrySecurity regSecurity = key.GetAccessControl();

                foreach (System.Security.AccessControl.AuthorizationRule rule in regSecurity.GetAccessRules(true, false, typeof(System.Security.Principal.NTAccount)))
                {
                    if ((System.Security.AccessControl.RegistryRights.Delete & ((System.Security.AccessControl.RegistryAccessRule)(rule)).RegistryRights) != System.Security.AccessControl.RegistryRights.Delete)
                    {
                        return false;
                    }
                }

                return true;
            }

        }
        catch (SecurityException)
        {
            return false;
        }
    }
///检查我们是否有删除注册表项的权限
/// 
///注册表项
///如果我们可以删除它,则为True
公共静态bool CanDeleteKey(注册表键)
{
尝试
{
如果(key.SubKeyCount>0)
{
bool-ret=假;
foreach(key.GetSubKeyNames()中的字符串子键)
{
ret=CanDeleteKey(key.OpenSubKey(subKey));
如果(!ret)
打破
}
返回ret;
}
其他的
{
System.Security.AccessControl.RegistrySecurity regSecurity=key.GetAccessControl();
foreach(regSecurity.GetAccessRules中的System.Security.AccessControl.AuthorizationRule规则(true、false、typeof(System.Security.Principal.NTAccount)))
{
如果((System.Security.AccessControl.RegistryRights.Delete&((System.Security.AccessControl.RegistryAccessRule)(规则)).RegistryRights)!=System.Security.AccessControl.RegistryRights.Delete)
{
返回false;
}
}
返回true;
}
}
catch(SecurityException)
{
返回false;
}
}

我想通过尝试删除密钥来查看我是否有删除权限,而不执行此操作。您仍然应该编写
try
/
catch
块。理论上,其他一些系统可以在您的检查和实际删除之间更改权限或向您检查的密钥添加新的子密钥。