///<summary> ///通过路径获取图标 ///</summary> ///<param name="path">文件或文件夹路径</param> ///<param name="uFlags">uFlags:GetFileInfoFlags.SHGFI_SMALLICON小图标;GetFileInfoFlags.SHGFI_LARGEICON大图标</param> ///<returns>获取的图标</returns> public static Icon GetIcon(string path, GetFileInfoFlags uFlags) { FileInfomation info = new FileInfomation(); GetFileInfo(path, 0, ref info, Marshal.SizeOf(info), (int)(GetFileInfoFlags.SHGFI_ICON | uFlags)); try { return Icon.FromHandle(info.hIcon); } catch { return null; } }
///<summary> ///通过路径获取小图标 ///</summary> ///<param name="path">文件或文件夹路径</param> ///<returns>获取的图标</returns> ///<remarks> ///效果不好,建议使用GetIcon方法来获取<see cref="Icon"/>实例,通过ToBitmap来得到<see cref="T:Bitmap"/>对象。 ///</remarks> public static Bitmap GetBitmap(string path) { FileInfomation info = new FileInfomation(); GetFileInfo(path, 0, ref info, Marshal.SizeOf(info), (int)(GetFileInfoFlags.SHGFI_ICON | GetFileInfoFlags.SHGFI_SMALLICON)); try { return Bitmap.FromHicon(info.hIcon); } catch { return null; } }
public static extern int GetFileInfo(string pszPath, int dwFileAttributes, ref FileInfomation psfi, int cbFileInfo, int uFlags);