已解决
添加引用System.Drawing
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Drawing;
- namespace test8
- {
- class FileIcon
- {
- [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;
- }
- public enum SHGFI
- {
- SHGFI_ICON = 0x100,
- SHGFI_LARGEICON = 0x0,
- SHGFI_USEFILEATTRIBUTES = 0x10
- }
- [DllImport("shell32.dll")]
- public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
- /// <summary>
- /// 获取文件图标
- /// </summary>
- /// <param name="p_Path">文件路径</param>
- /// <returns></returns>
- public Icon GetFileIcon(string p_Path)
- {
- SHFILEINFO _SHFILEINFO = new SHFILEINFO();
- IntPtr _IconIntPtr = SHGetFileInfo(p_Path, 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES));
- if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
- Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
- return _Icon;
- }
- /// <summary>
- /// 获取文件夹图标
- /// </summary>
- /// <returns></returns>
- public Icon GetDirectoryIcon()
- {
- SHFILEINFO _SHFILEINFO = new SHFILEINFO();
- IntPtr _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON));
- if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
- Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
- return _Icon;
- }
- }
- }
复制代码 |