[英]find icon for .exe
I have the path name to a .exe file. 我有一个.exe文件的路径名。
How do I find the path to the icon that windows would use if this file were dropped on the desktop? 如果将此文件放置在桌面上,如何找到Windows使用的图标的路径? (I want to display this icon in my own program.)
(我想在自己的程序中显示此图标。)
I need to be able to find the icon via my C# program at runtime. 我需要能够在运行时通过C#程序找到图标。
Using C# in Visual Studio 2008. 在Visual Studio 2008中使用C#。
If found this code online a while ago to do that: 如果前一段时间在网上找到此代码,则可以执行以下操作:
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;
};
you also need 你还需要
using System.Drawing;
using System.Runtime.InteropServices;
to reference all the appropriate classes 引用所有适当的类
The default icon would be the one embedded in the exe at index 0. 默认图标是嵌入在exe中索引0的图标。
Look at: Project properties > application tab > Icon. 查看:项目属性>应用程序选项卡>图标。
This CodeProject article seems to do the job pretty well. 这篇CodeProject文章似乎做得很好。 It provides an
IconExtractor
class that encapsulates all the Win32 API stuff for you into a nice managed interface. 它提供了一个
IconExtractor
类,该类将所有Win32 API内容封装为一个不错的托管接口。
You can use it as such: 您可以这样使用它:
using TKageyu.Utils;
...
using (IconExtractor ie = new IconExtractor(@"D:\sample.exe"))
{
Icon icon0 = ie.GetIcon(0);
Icon icon1 = ie.GetIcon(1);
...
Icon[] splitIcons = IconExtractor.SplitIcon(icon0);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.