簡體   English   中英

從 C# 中的注冊表中刪除與文件擴展名關聯的圖標

[英]Deleting the icon associated with the file extension from the registry in C#

我使用這樣的方法將文件擴展名與圖標相關聯:

public static void Associate(string extension, string progID, string description, string icon, string application)
{
    Registry.ClassesRoot.CreateSubKey(extension)?.SetValue("", progID);
    if (!string.IsNullOrEmpty(progID))
    {
        using var key = Registry.ClassesRoot.CreateSubKey(progID);
        if (description != null)
            key?.SetValue("", description);
        if (icon != null)
            key?.CreateSubKey("DefaultIcon")?.SetValue("", ToShortPathName(icon));
        if (application != null)
            key?.CreateSubKey(@"Shell\Open\Command")?.SetValue("", ToShortPathName(application) + " \"%1\"");
    }
}

當我想完全刪除我關聯的文件擴展名時,我不知道如何繼續。 為此,我嘗試了這樣的事情:

public static void Remove(string progID)
{
    Registry.ClassesRoot.DeleteSubKey(progID);
}

它給出了這樣的錯誤:

注冊表有子項,此方法不支持遞歸刪除。

它在這個地址有一個解決方案: https://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C

像這樣:

public static RegistryKey BaseRegistryKey { get; set; } = Registry.LocalMachine;
public static string subKey { get; set; } = "SOFTWARE\\" + Application.ProductName.ToUpper();

public bool DeleteSubKeyTree()
{
    try
    {
        // Setting
        RegistryKey rk = baseRegistryKey ;
        RegistryKey sk1 = rk.OpenSubKey(subKey);
        // If the RegistryKey exists, I delete it
        if ( sk1 != null )
            rk.DeleteSubKeyTree(subKey);

        return true;
    }
    catch (Exception e)
    {
        // AAAAAAAAAAARGH, an error!
        MessageBox.Show(e, "Deleting SubKey " + subKey);
        return false;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM