如何在Windows中检索任何给定文件类型的默认图标?

问题描述 投票:3回答:2

如何获取Windows默认使用的指定文件类型的图标?

c# .net
2个回答
2
投票

试试这个

    public static Hashtable GetTypeAndIcon()
    {
        try
        {
            RegistryKey icoRoot = Registry.ClassesRoot;
            string[] keyNames = icoRoot.GetSubKeyNames();
            Hashtable iconsInfo = new Hashtable();

            foreach (string keyName in keyNames)
            {
                if (String.IsNullOrEmpty(keyName)) continue;
                int indexOfPoint = keyName.IndexOf(".");

                if (indexOfPoint != 0) continue;

                RegistryKey icoFileType = icoRoot.OpenSubKey(keyName);
                if (icoFileType == null) continue;

                object defaultValue = icoFileType.GetValue("");
                if (defaultValue == null) continue;

                string defaultIcon = defaultValue.ToString() + "\\DefaultIcon";
                RegistryKey icoFileIcon = icoRoot.OpenSubKey(defaultIcon);
                if (icoFileIcon != null)
                {
                    object value = icoFileIcon.GetValue("");
                    if (value != null)
                    {
                        string fileParam = value.ToString().Replace("\"", "");
                        iconsInfo.Add(keyName, fileParam);
                    }
                    icoFileIcon.Close();
                }
                icoFileType.Close();
            }
            icoRoot.Close();
            return iconsInfo;
        }
        catch (Exception exc)
        {
            throw exc;
        }
    }

1
投票

文件类型的默认图标,无论您是否碰巧有其中一种类型的文件,都在Window的注册表中定义

HKEY_CLASSES_ROOT \ type \ DefaultIcon(默认)

...但是当VirtualBlackFox在他对nice c# code sample的回答中有一个How do I get common file type icons in C#?时,为什么要修补它?

© www.soinside.com 2019 - 2024. All rights reserved.