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

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

+3


source to share


3 answers


try it



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

      

+2


source


The default icons for the file type , whether or not you have one of these file types, handy are defined in the Windows Registry under

HKEY_CLASSES_ROOT \ type \ DefaultIcon (default)



... but why bother with that when there is a good example of C # code here on VirtualBlackFox in his answer to How to get common file type icons in C #?

+1


source


I like my answer better, plus I hate righteous coders like you.

0


source







All Articles