/// <summary> /// Get the associated Icon for a file or application, this method always returns /// an icon. If the strPath is invalid or there is no idonc the default icon is returned /// </summary> /// <param name="strPath">full path to the file</param> /// <param name="bSmall">if true, the 16x16 icon is returned otherwise the 32x32</param> /// <returns></returns> public static Icon GetIcon(string strPath, bool bSmall) { lock (getIconState) { ShFileInfo info = new ShFileInfo(true); int cbFileInfo = Marshal.SizeOf(info); ShgFi flags; if (bSmall) { flags = ShgFi.Icon | ShgFi.SmallIcon | ShgFi.UseFileAttributes; } else { flags = ShgFi.Icon | ShgFi.LargeIcon | ShgFi.UseFileAttributes; } SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags); return(Icon.FromHandle(info.hIcon)); } }
/// <summary> /// Get the associated Icon for a file or application, this method always returns /// an icon. If the strPath is invalid or there is no idonc the default icon is returned /// </summary> /// <param name="strPath">full path to the file</param> /// <param name="bSmall">if true, the 16x16 icon is returned otherwise the 32x32</param> /// <returns></returns> public static Icon GetIcon(string strPath, bool bSmall) { lock (getIconState) { ShFileInfo info = new ShFileInfo(true); int cbFileInfo = Marshal.SizeOf(info); ShgFi flags; if (bSmall) { flags = ShgFi.Icon | ShgFi.SmallIcon | ShgFi.UseFileAttributes; } else { flags = ShgFi.Icon | ShgFi.LargeIcon | ShgFi.UseFileAttributes; } SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags); return Icon.FromHandle(info.hIcon); } }
private static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, out ShFileInfo psfi, uint cbfileInfo, ShgFi uFlags);