在C#中,如何删除"Explorer file associate"覆盖"Registry Classes"文件关联?



如何以编程方式删除资源管理器创建的所有"文件关联",以便HKCR下的文件关联正常工作?

例如,我想删除与单击"打开方式..."的用户对应的注册表项。在资源管理器中。例:

HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.txt

或其他文件扩展名,例如.py或.pl。

我写了一些 c# 代码来做到这一点...但是它会引发异常。 这是我正在尝试做的:

    ExplorerOverrideDelete(".txt");
    // Throws Exception:
    //  System.UnauthorizedAccessException: Cannot write to the registry key.

以下是函数:

    public static void ExplorerOverrideDelete (string ext)
    {
        if (!ext.StartsWith(".")) {
            ext = "." + ext;
        }
        var hivereg = GetHive("HKCU");
        var key     = hivereg.OpenSubKey(
            @"SoftwareMicrosoftWindowsCurrentVersionExplorerFileExts"
                       + ext);
        if (key == null)
            return;
        var keyroot     = hivereg.OpenSubKey(
            @"SoftwareMicrosoftWindowsCurrentVersionExplorerFileExts");
        if (keyroot == null)
            return;
        keyroot.DeleteSubKeyTree(ext);
        key.Dispose();
        keyroot.Dispose();
        hivereg.Dispose();
    }
    private static RegistryKey GetHive(string hive) {
        switch(hive.ToUpper()) {
            case "HKCU": return Registry.CurrentUser;
            case "HKLM": return Registry.LocalMachine;
            case "HKCR": return Microsoft.Win32.Registry.ClassesRoot;
        }
        return null;
    }

更多信息:

PS C:UsersjohnDocuments> get-acl HKCU:SoftwareMicrosoftWindowsCurrentVersionExplorerFileExts | fl

Path   : Microsoft.PowerShell.CoreRegistry::HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileE
         xts
Owner  : DESKTOP-D3USQOTjohn
Group  : DESKTOP-D3USQOTNone
Access : DESKTOP-D3USQOTjohn Allow  FullControl
         NT AUTHORITYSYSTEM Allow  FullControl
         BUILTINAdministrators Allow  FullControl
         NT AUTHORITYRESTRICTED Allow  ReadKey
         APPLICATION PACKAGE AUTHORITYALL APPLICATION PACKAGES Allow  R
PS C:UsersjohnDocuments> get-acl HKCU:SoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.txt | fl
Path   : Microsoft.PowerShell.CoreRegistry::HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerFileE
         xts.txt
Owner  : DESKTOP-D3USQOTjohn
Group  : DESKTOP-D3USQOTNone
Access : DESKTOP-D3USQOTjohn Allow  FullControl
         NT AUTHORITYSYSTEM Allow  FullControl
         BUILTINAdministrators Allow  FullControl
         NT AUTHORITYRESTRICTED Allow  ReadKey
         APPLICATION PACKAGE AUTHORITYALL APPLICATION PACKAGES

下面是对我有用的代码。 我仍然不完全明白为什么我需要以管理员身份运行它,因为当前用户的 ACL 具有完全控制权。 如果有人对此有任何想法,请在下面给我喊一声。谢谢。

public static void ExplorerOverrideDelete (string ext)
{
    if (!IsAdministrator()) {
        MessageBox.Show(
            "ExplorerOverrideDelete(): Rerun program as Admin",
            "Access Denied"
    }
    if (!ext.StartsWith(".")) {
        ext = "." + ext;
    }
    var keyroot     = Registry.CurrentUser.OpenSubKey(
        @"SoftwareMicrosoftWindowsCurrentVersionExplorerFileExts", 
        RegistryKeyPermissionCheck.ReadWriteSubTree);
    if (keyroot == null)
        return;
    keyroot.GetAccessControl(System.Security.AccessControl.AccessControlSections.All);
    // /keyroot.GetAccessControl(
    //     System.Security.AccessControl.AccessControlSections.All
    //  & ~System.Security.AccessControl.AccessControlSections.Audit);
    try {
        keyroot.DeleteSubKeyTree(ext);
    }
    catch(Exception e) {
        keyroot.Close();
        hivereg.Close();            
        MessageBox.Show(e.Message);
    }
    keyroot.Close();
    hivereg.Close();
}
public static bool IsAdministrator()
{
    System.Security.Principal.WindowsIdentity identity
        = System.Security.Principal.WindowsIdentity.GetCurrent();
    System.Security.Principal.WindowsPrincipal principal
        = new System.Security.Principal.WindowsPrincipal(identity);
    return principal.IsInRole(
        System.Security.Principal.WindowsBuiltInRole.Administrator
    );
}

最新更新