把下面的代码Copy到Class下面
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 [StructLayout(LayoutKind.Sequential)]
2 public struct SHFILEINFO
3 {
4 public IntPtr hIcon;
5 public IntPtr iIcon;
6 public uint dwAttributes;
7 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
8 public string szDisplayName;
9 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
10 public string szTypeName;
11 };
12
13 class Win32
14 {
15 public const uint SHGFI_ICON = 0x100;
16 public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
17 public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
18
19 [DllImport("shell32.dll")]
20 public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
21 }
通过SHGetFileInfo API 来获得文件的图标.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 private Icon GetFileIcon(string fullFileName)
2 {
3 IntPtr hImgSmall; //the handle to the system image list
4 //IntPtr hImgLarge; //the handle to the system image list
5 //string fName; // 'the file name to get icon from
6 SHFILEINFO shinfo = new SHFILEINFO();
7
8
9
10 //Use this to get the small Icon
11 hImgSmall = Win32.SHGetFileInfo(fullFileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
12
13 //Use this to get the large Icon
14 //hImgLarge = SHGetFileInfo(fName, 0,
15 // ref shinfo, (uint)Marshal.SizeOf(shinfo),
16 // Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
17
18 //The icon is returned in the hIcon member of the shinfo struct
19 System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
20
21 return myIcon;
22
23
24 }