Amirshk Amirshk - 1 year ago 112
C# Question

Get the icon for a given extension

I know i can extract a file's icon using

using (System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(filePath))
icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(

But how can I, with no file, get the icon for a given extension?

Answer Source

Use the GetFileIcon method from this CodeProject article from Paul Ingles and pass .ext as the name parameter.

The GetFileIcon method is a wrapper around the native SHGetFileInfo and copied here for illustration:

public static System.Drawing.Icon GetFileIcon(string name, IconSize size, 
                                              bool linkOverlay)
    Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
    uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

    if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY;

    /* Check the size specified for return. */
    if (IconSize.Small == size)
        flags += Shell32.SHGFI_SMALLICON ; // include the small icon flag
        flags += Shell32.SHGFI_LARGEICON ;  // include the large icon flag

    Shell32.SHGetFileInfo( name, 
        ref shfi, 
        (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi), 
        flags );

    // Copy (clone) the returned icon to a new object, thus allowing us 
    // to call DestroyIcon immediately
    System.Drawing.Icon icon = (System.Drawing.Icon)
    User32.DestroyIcon( shfi.hIcon ); // Cleanup
    return icon;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download