通过winApi 由文件名,获取文件图标

 1         [DllImport("Shell32.dll")]
2 static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
3
4 struct SHFILEINFO
5 {
6 public IntPtr hIcon;
7 public int iIcon;
8 public uint dwAttributes;
9 public char szDisplayName;
10 public char szTypeName;
11 }
12
13 /// <summary>
14      /// 从文件扩展名得到文件关联图标
15      /// </summary>
16      /// <param name="fileName">文件名或文件扩展名</param>
17      /// <param name="smallIcon">是否是获取小图标,否则是大图标</param>
18      /// <returns>图标</returns>
19 static public Icon GetFileIcon(string fileName, bool smallIcon)
20 {
21 SHFILEINFO fi = new SHFILEINFO();
22 Icon ic = null;
224 int iTotal = (int)SHGetFileInfo(fileName, 100, ref fi, 0, (uint)(smallIcon ? 273 : 272));
25 if (iTotal > 0)
26 {
27 ic = Icon.FromHandle(fi.hIcon);
28 }
29 return ic;
30 }

你可能感兴趣的:(api)