C# 目前找到的方法有2中
一种为:
Icon icon = Icon.ExtractAssociatedIcon(fileFullPath);
这种不怎么靠谱,有些文件的ICO干脆为空白
一种为:
[StructLayout(LayoutKind.Sequential)] public struct SHFILEINFO { public IntPtr hIcon; public IntPtr iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; }; class Win32 { public const uint SHGFI_ICON = 0x100; public const uint SHGFI_LARGEICON = 0x0; // 'Large icon public const uint SHGFI_SMALLICON = 0x1; // 'Small icon [DllImport("shell32.dll")] public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); [DllImport("shell32.dll")] public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons); }
SHFILEINFO shinfo = new SHFILEINFO(); Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON); System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon); FileStream fileStream = new FileStream(path, FileMode.Create); myIcon.Save(fileStream); fileStream.Close();
第二种比第一种好点,但是也有些不正确
有没有大神还有其他的方法,比如360软件小助手中,管理文件获取的图标很清晰
帮你找了一个,有第三种方法,补充第二种方法的不足。
发现用了效果和第二种一样的,没有360软件小助手那么准确,不过还是谢谢了