【问题标题】:How do I retrieve the default icon for any given file type in Windows?如何在 Windows 中检索任何给定文件类型的默认图标?
【发布时间】:2014-10-11 04:29:55
【问题描述】:

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

【问题讨论】:

  • @sukhi 这给出了一个已知文件的图标。我认为 OP 想要与给定文件类型关联的图标。 “如果我们有文件扩展名列表”
  • @MickyDuncan 不,这不仅仅是给定文件的图标。当您只有文件扩展名时,接受的答案和投票最多的答案也会给出图标。
  • @Aeron 没有 API 可以像在全新安装中那样获取 Windows 默认图标,但是有 API 可以像在当前安装中那样获取 Windows 图标。我已将其作为副本关闭,因为您说后者也可以,即使您真的要前者。
  • @Aeron 好的,重新打开,这仍然是一个公平的问题,但在这种情况下,请编辑您的问题以澄清您在问什么。 Micky Duncan 的回答确实试图回答当前安装中的 Windows 图标,因此如果您的问题明确说明这 不是 您所要求的,这对其他人会有所帮助。

标签: c# .net


【解决方案1】:

试试这个

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

【讨论】:

  • 你如何使用这些路径在 c# 中实际显示一个图标?
【解决方案2】:

文件类型的默认图标无论您是否碰巧手边有这些类型的文件,都在 Window's Registry 下的

中定义

HKEY_CLASSES_ROOT\ 类型 \DefaultIcon(默认)

...但是当 VirtualBlackFox 在他对How do I get common file type icons in C#?

的回答中有一个nice c# code sample 时,为什么要修补它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2016-02-16
    相关资源
    最近更新 更多