private static Icon _GetIcon(string strPath, bool largeIcon = false) { SHFILEINFO info = new SHFILEINFO(); int cbFileInfo = Marshal.SizeOf(info); Shell32.SHGetFileInfo(strPath, 256, ref info, (uint)cbFileInfo, (uint)SHGetFileInfoConstants.SHGFI_SYSICONINDEX); Icon retVal = null; try { IntPtr hIL = IntPtr.Zero; int iImageList = largeIcon ? 0x2 /*SHIL_EXTRALARGE*/ : 0x1 /*SHIL_SMALL*/; Guid riid = Shell32.IID_IImageList; if (Shell32.SHGetImageList(iImageList, ref riid, ref hIL) == WinError.S_OK && hIL != IntPtr.Zero) { IntPtr hIcon = ComCtl32.ImageList_GetIcon(hIL, info.iIcon, 0); if (hIcon != IntPtr.Zero) { retVal = Icon.FromHandle(hIcon).Clone() as Icon; User32.DestroyIcon(hIcon); } } } catch { } return retVal; }
public static extern IntPtr SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);