题
我的路径名.exe文件。
如何找到的路径,如果该文件是在桌面上丢弃的窗口将使用的图标? (我想显示在自己的程序该图标。)
我需要能够通过在运行时我的C#程序找到图标。
在Visual Studio 2008使用C#。
解决方案
如果发现这个代码在网上前一段时间做的:
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;
};
您还需要
using System.Drawing;
using System.Runtime.InteropServices;
引用所有适当的类
其他提示
默认图标将是一个在索引0处嵌入在该exe。
看:项目属性>应用标签>图标
这CodeProject上的文章似乎做的工作相当不错。它提供了封装了所有的Win32 API的东西对你的IconExtractor
类到一个不错的管理界面。
可以使用它作为这样:
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);
...
}
不隶属于 StackOverflow