用途:用于获取文件系统默认图标
-
-
using System.Collections.Generic;
-
-
-
-
using System.Runtime.InteropServices;
-
-
-
namespace HebianGu.ComLibModule.API
-
-
-
-
public partial class IconHelper
-
-
-
-
-
-
-
-
-
[DllImport("shell32.dll")]
-
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
-
-
-
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);
-
-
-
-
-
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);
-
-
-
-
-
public Icon GetSystemInfoIcon(string p_Path)
-
-
if (Path.HasExtension(p_Path))
-
-
-
-
return Icon.ExtractAssociatedIcon(p_Path);
-
-
-
-
-
-
-
-
-
-
return this.GetDirectoryIcon();
-
-
-
-
-
-
-
-
-
-
-
private static IconHelper t = null;
-
-
-
private static object localLock = new object();
-
-
-
public static IconHelper Instance
-
-
-
-
-
-
-
-
-
return t = new IconHelper();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
[StructLayout(LayoutKind.Sequential)]
-
-
-
-
-
public uint dwAttributes;
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
-
public string szDisplayName;
-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
-
public string szTypeName;
-
-
-
-
-
-
-
-
SHGFI_USEFILEATTRIBUTES = 0x10
-
-
应用示例:
-
-
-
-
get { return IconHelper.Instance.GetSystemInfoIcon(FilePath); }
-