原文:
地址:http://www.flawlesscode.com/post/2008/02/Getting-the-icon-for-a-given-file-extension.aspx
代码下载:http://www.flawlesscode.com/file.axd?file=IconUtils.zip
主要代码:
using
System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace FlawlessCode.IconUtils
{
public static class IconUtils
{
/// <summary>
/// Usedtoaccesssystemfoldericons.
/// </summary>
/// <paramname="largeIcon"> Specifylargeorsmallicons. </param>
/// <paramname="openFolder"> SpecifyopenorclosedFolderType. </param>
/// <returns> TherequestedIcon. </returns>
public static IconGetIconForFolder(BooleanlargeIcon,BooleanopenFolder)
{
SHFILEINFOshellFileInfo = 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>
/// Usedtoaccessfileiconsforagivenextension.
/// </summary>
/// <paramname="extension"> Thefileextensiontogettheiconfor. </param>
/// <paramname="largeIcon"> Specifylargeorsmallicons. </param>
/// <paramname="linkOverlay"> Specifylinkoverlayontheicon. </param>
/// <returns> TherequestedIcon </returns>
public static IconGetIconForFileExtension(Stringextension,BooleanlargeIcon,BooleanlinkOverlay)
{
if ( ! extension.StartsWith( " . " ))
extension = " . " + extension;
SHFILEINFOshellFileInfo = 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 IntPtrhIcon;
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 IntPtrSHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFOpsfi,
uint cbFileInfo,
uint uFlags
);
[DllImport( " User32.dll " )]
private static extern int DestroyIcon(IntPtrhIcon);
#endregion
}
}
using System.Drawing;
using System.Runtime.InteropServices;
namespace FlawlessCode.IconUtils
{
public static class IconUtils
{
/// <summary>
/// Usedtoaccesssystemfoldericons.
/// </summary>
/// <paramname="largeIcon"> Specifylargeorsmallicons. </param>
/// <paramname="openFolder"> SpecifyopenorclosedFolderType. </param>
/// <returns> TherequestedIcon. </returns>
public static IconGetIconForFolder(BooleanlargeIcon,BooleanopenFolder)
{
SHFILEINFOshellFileInfo = 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>
/// Usedtoaccessfileiconsforagivenextension.
/// </summary>
/// <paramname="extension"> Thefileextensiontogettheiconfor. </param>
/// <paramname="largeIcon"> Specifylargeorsmallicons. </param>
/// <paramname="linkOverlay"> Specifylinkoverlayontheicon. </param>
/// <returns> TherequestedIcon </returns>
public static IconGetIconForFileExtension(Stringextension,BooleanlargeIcon,BooleanlinkOverlay)
{
if ( ! extension.StartsWith( " . " ))
extension = " . " + extension;
SHFILEINFOshellFileInfo = 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 IntPtrhIcon;
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 IntPtrSHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFOpsfi,
uint cbFileInfo,
uint uFlags
);
[DllImport( " User32.dll " )]
private static extern int DestroyIcon(IntPtrhIcon);
#endregion
}
}