public static Image FileAssociatedImage(string path, bool isLarge, bool isExist) { SHFILEINFO fileInfo = new SHFILEINFO(); uint flags = SHGFI_ICON; if (!isLarge) flags |= SHGFI_SMALLICON; if (!isExist) flags |= SHGFI_USEFILEATTRIBUTES; try { SHGetFileInfo(path, 0, ref fileInfo, (uint)Marshal.SizeOf(fileInfo), flags); if (fileInfo.hIcon == IntPtr.Zero) { return null; } else { return Icon.FromHandle(fileInfo.hIcon).ToBitmap(); } } finally { if (fileInfo.hIcon != IntPtr.Zero) { DestroyIcon(fileInfo.hIcon); } } }
public static Image getFolderIcon(string filePath) { SHFILEINFO shinfo = new SHFILEINFO(); IntPtr imageLarge = Win32.SHGetFileInfo( filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON); return Icon.FromHandle(shinfo.hIcon).ToBitmap(); }
public static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);