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

本文关键字:类型 文件 默认 图标 Windows 任何 检索 | 更新日期: 2023-09-27 18:18:21

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

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

文件类型的默认图标,无论您是否碰巧手边有这些类型的文件之一,都在窗口的注册表

下定义

HKEY_CLASSES_ROOT' type 'DefaultIcon(Default)

…但是当VirtualBlackFox在他的回答中有一个很好的c#代码示例时,为什么要修补它呢?

试试这个

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