If you want to extract the icon from an extension, you can do :
SHFILEINFO sfi = new SHFILEINFO();
IntPtr hImageList = SHGetFileInfo(".cs", 0, ref sfi, (uint)Marshal.SizeOf(sfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES);
Icon ico = Icon.FromHandle(sfi.hIcon);
// Test displaying icon on desktop
using (Graphics gr = Graphics.FromHwnd(IntPtr.Zero))
{
gr.DrawIcon(ico, 50, 50);
}
with declarations :
public const int SHGFI_ICON = 0x100; // Get icon
public const int SHGFI_DISPLAYNAME = 0x200; // Get display name
public const int SHGFI_TYPENAME = 0x400; // Get type name
public const int SHGFI_ATTRIBUTES = 0x800; // Get attributes
public const int SHGFI_ICONLOCATION = 0x1000; // Get icon location
public const int SHGFI_EXETYPE = 0x2000; // Return exe type
public const int SHGFI_SYSICONINDEX = 0x4000; // Get system icon index
public const int SHGFI_LINKOVERLAY = 0x8000; // put a link overlay On icon
public const int SHGFI_SELECTED = 0x10000; // Show() icon In selected state
public const int SHGFI_ATTR_SPECIFIED = 0x20000; // Get only specified attributes
public const int SHGFI_LARGEICON = 0x0; // Get large icon
public const int SHGFI_SMALLICON = 0x1; // Get small icon
public const int SHGFI_OPENICON = 0x2; // Get open icon
public const int SHGFI_SHELLICONSIZE = 0x4; // Get shell size icon
public const int SHGFI_PIDL = 0x8; // pszPath Is a pidl
public const int SHGFI_USEFILEATTRIBUTES = 0x10; // use passed dwFileAttribute
public const int SHGFI_ADDOVERLAYS = 0x20; // apply the appropriate overlays
public const int SHGFI_OVERLAYINDEX = 0x40; // Get the index Of the overlay in the upper 8 bits of the iIcon
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
[DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);