http://www.kennyandkarin.com/Kenny/CodeCorner/Tools/IconBrowser/
我这里有一段代码,可获取任意文件的图标:
using System;
using System.Runtime.InteropServices;
using System.Drawing;
namespace WindowsAppTmp
{
public class ExtractIcon
{
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon=IntPtr.Zero;iIcon=0;dwAttributes=0;szDisplayName="";szTypeName="";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst=260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst=80)]
public string szTypeName;
};
public static Icon GetIcon(string strPath, bool bSmall)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon|SHGFI.SmallIcon|SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon|SHGFI.LargeIcon|SHGFI.UseFileAttributes;
SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}
}
using System;
using System.Windows.Forms;
using System.Drawing;
namespace MyForm {
public class CreatedForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button;
private Label label;
public static void Main( )
{
Application.Run( new CreatedForm( ) );
}
public CreatedForm()
{
InitializeComponents();
}
void InitializeComponents() {
//
// Set up generated class form
//
this.SuspendLayout();
this.Name = "form";
this.Size = new System.Drawing.Size(200, 96);
this.Text = "Test";
Obtaining (and managing) file and folder icons using SHGetFileInfo in C#
http://www.codeproject.com/csharp/fileicon.asp
HOW TO: Use the SHGetFileInfo Function to Get the Icons That Are Associated with Files in Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;319350