How do I get the icon that Windows uses by default for a specified file type?
Asked
Active
Viewed 2,448 times
3
-
2see the following link [http://stackoverflow.com/questions/462270/get-file-icon-used-by-shell][1] – sukhi Aug 18 '14 at 09:44
-
1@sukhi That gives the icon for a known file. The OP I believe wants the icon associated with a **given file type**. _"If we have a list of file extensions"_ – Aug 18 '14 at 09:48
-
@MickyDuncan No, that doesn't just give the icon for a given file. Both the accepted answer and the top-voted answer there also give icons when you just have a file extension. – Aug 18 '14 at 10:03
-
@Aeron There's no API to get the Windows default icons as they would be on a clean installation, but there are APIs to get the Windows icons as they are on the current installation. I've closed this as a duplicate because you say the latter is also okay, even though you're really asking for the former. – Aug 18 '14 at 10:06
-
@Aeron Okay, re-opened, that's still a fair question, but in that case, please do edit your question to clarify what it is you're asking. Micky Duncan's answer does attempt to answer for the Windows icons as they are on the current installation, so it'd be helpful for others if your question is explicit in that that's *not* what you're asking for. – Aug 18 '14 at 10:13
-
@Aeron What you've updated your question to is exactly what's answered in the other question (not sukhi's link). I thought you wanted to have your question re-opened because you were interested in what you literally asked, in other words, *not* how they look on the current installation, but how they look by default? Did I misunderstand? – Aug 18 '14 at 10:25
2 Answers
1
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#?

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

MarkKiessling
- 19
- 1
- 11