public static Icon GetIconForFile(string filename, ShellIconSize size)
{
SHFILEINFO shinfo = new SHFILEINFO();
NativeMethods.SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), size);
Icon icon = null;
if (shinfo.hIcon.ToInt32() != 0)
{
// create the icon from the native handle and make a managed copy of it
icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
// release the native handle
NativeMethods.DestroyIcon(shinfo.hIcon);
}
return icon;
}