using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace CommonLibrary
{
/// <summary>
/// 根据文件名称获取文件的系统图标
/// </summary>
public static class IconUtilsByFilePath
{
[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;
}
///<summary>
/// 返回系统设置的图标
///</summary>
///<param name="pszPath">文件路径 如果为"" 返回文件夹的</param>
///<param name="dwFileAttributes">0</param>
///<param name="psfi">结构体</param>
///<param name="cbSizeFileInfo">结构体大小</param>
///<param name="uFlags">枚举类型</param>
///<returns>-1失败</returns>
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
public enum SHGFI
{
SHGFI_ICON = 0x100,
SHGFI_LARGEICON = 0x0,
SHGFI_USEFILEATTRIBUTES = 0x10
}
///<summary>
/// 获取文件图标 zgke@sina.com qq:116149
///</summary>
///<param name="p_Path">文件全路径</param>
///<returns>图标</returns>
public static 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>
/// 获取文件夹图标 zgke@sina.com qq:116149
///</summary>
///<returns>图标</returns>
public static 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;
}
}
/// <summary>
/// 根据文件的后缀获取文件的系统图标
/// </summary>
public static class IconUtilsByExtension
{
///<summary>
/// Used to access system folder icons.
///</summary>
///<param name="largeIcon">Specify large or small icons.</param>
///<param name="openFolder">Specify open or closed FolderType.</param>
///<returns>The requested Icon.</returns>
public static Icon GetIconForFolder(Boolean largeIcon, Boolean openFolder)
{
SHFILEINFO shellFileInfo = new SHFILEINFO();
try
{
uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
flags |= openFolder ? SHGFI_OPENICON : 0;
flags |= largeIcon ? SHGFI_LARGEICON : SHGFI_SMALLICON;
SHGetFileInfo(null,
FILE_ATTRIBUTE_DIRECTORY,
ref shellFileInfo,
(uint)Marshal.SizeOf(shellFileInfo),
flags);
return (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone();
}
finally
{
DestroyIcon(shellFileInfo.hIcon); // Cleanup
}
}
///<summary>
/// Used to access file icons for a given extension.
///</summary>
///<param name="extension">The file extension to get the icon for.</param>
///<param name="largeIcon">Specify large or small icons.</param>
///<param name="linkOverlay">Specify link overlay on the icon.</param>
///<returns>The requested Icon</returns>
public static Icon GetIconForFileExtension(String extension, Boolean largeIcon, Boolean linkOverlay)
{
if (!extension.StartsWith("."))
extension = "." + extension;
SHFILEINFO shellFileInfo = new SHFILEINFO();
try
{
uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
flags |= linkOverlay ? SHGFI_LINKOVERLAY : 0;
flags |= largeIcon ? SHGFI_LARGEICON : SHGFI_SMALLICON;
SHGetFileInfo(extension,
FILE_ATTRIBUTE_NORMAL,
ref shellFileInfo,
(uint)Marshal.SizeOf(shellFileInfo),
flags);
return (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone();
}
finally
{
DestroyIcon(shellFileInfo.hIcon);
}
}
#region P/Invoke
private const int MAX_PATH = 256;
private const uint SHGFI_ICON = 0x000000100;
private const uint SHGFI_LINKOVERLAY = 0x000008000;
private const uint SHGFI_LARGEICON = 0x000000000;
private const uint SHGFI_SMALLICON = 0x000000001;
private const uint SHGFI_OPENICON = 0x000000002;
private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public const int NAMESIZE = 80;
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)]
public string szTypeName;
};
[DllImport("Shell32.dll")]
private static extern IntPtr SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbFileInfo,
uint uFlags
);
[DllImport("User32.dll")]
private static extern int DestroyIcon(IntPtr hIcon);
#endregion
}
}