找到.exe的图标

Mar*_*k T 1 c# icons exe find locate

我有一个.exe文件的路径名.

如果将此文件放在桌面上,如何找到Windows将使用的图标路径?(我想在我自己的程序中显示这个图标.)

我需要能够在运行时通过我的C#程序找到图标.

在Visual Studio 2008中使用C#.

JDu*_*ley 6

如果在不久前在网上找到这个代码那么做:

class Win32
{
    public const uint SHGFI_ICON = 0x100;
    public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
    public const uint SHGFI_SMALLICON = 0x1; // 'Small icon

    [DllImport("shell32.dll")]
    public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);

    public static Bitmap GetFileIcon(string fName)
    {
        IntPtr hImgSmall; //the handle to the system image list
        SHFILEINFO shinfo = new SHFILEINFO();
        hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
        return Icon.FromHandle(shinfo.hIcon).ToBitmap();
    }
}


[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;
};
Run Code Online (Sandbox Code Playgroud)

你还需要

using System.Drawing;
using System.Runtime.InteropServices;
Run Code Online (Sandbox Code Playgroud)

引用所有适当的类