private Icon GetFileIcon(string fileName, IconSize size) { IntPtr iconPtr; Icon ret = null; SHFILEINFO shinfo = new SHFILEINFO(); uint flags = Win32.SHGFI_ICON; /* Check the size specified for return. */ if (size == IconSize.Small) flags += Win32.SHGFI_SMALLICON; else flags += Win32.SHGFI_LARGEICON; iconPtr = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags); if (iconPtr != IntPtr.Zero) { // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly ret = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone(); // Cleanup Win32.DestroyIcon(shinfo.hIcon); } return ret; }
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);