public static Icon GetFileIcon(string fileName, bool isLargeIcon) { SHFILEINFO shfi = new SHFILEINFO(); IntPtr hI; if (fileName == "文件夹") { fileName = "C:\\Windows\\"; //fileName = "E:\\work\\video\\m7.flv"; SHGetFileInfo(fileName, (uint)0x80, ref shfi, (uint)Marshal.SizeOf(shfi),//Marshal.SizeOf返回对象的非托管大小 (uint)(0x100 | 0x400 | SHGFI_SMALLICON));//取得icon和typename } else { if (isLargeIcon) hI = SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_LARGEICON); else hI = SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON); } Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon; DestroyIcon(shfi.hIcon); //释放资源 return icon; }
static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);