jay_t55
jay_t55

Reputation: 11652

How do I retrieve the default icon for any given file type in Windows?

How do I get the icon that Windows uses by default for a specified file type?

Upvotes: 3

Views: 2855

Answers (2)

MarkKiessling
MarkKiessling

Reputation: 19

Try this

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

Upvotes: 1

user585968
user585968

Reputation:

Default icons for a file type irrespective of whether you happen to have one of these types of files handy, are defined in the Window's Registry under

HKEY_CLASSES_ROOT\ type \DefaultIcon(Default)

...but why tinker with that when there is a nice c# code sample here by VirtualBlackFox in his answer to How do I get common file type icons in C#?

Upvotes: 1

Related Questions