获取指定文件的图标

[System.Runtime.InteropServices.DllImport("shell32")]
private static extern IntPtr ExtractAssociatedIcon(IntPtr hInst,string lpIconPath,ref int lpiIcon);

static IntPtr hIcon;

private void Form1_Load(object sender, System.EventArgs e)
{
    int IconIndex=0;
    hIcon=ExtractAssociatedIcon(this.Handle,Application.ExecutablePath,ref IconIndex);
}

//在窗体上画出来
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    Icon icon=Icon.FromHandle(hIcon);
    e.Graphics.DrawIcon(icon,10,10);
}

注解:

HICON ExtractAssociatedIcon(         
    HINSTANCE hInst,      应用程序实例
    LPTSTR lpIconPath,    文件的全路径
    LPWORD lpiIcon        文件中icon的索引
);
If the function succeeds, the return value is an icon handle
Contributors: FHL