如何从可执行文件中获取图标,该文件仅在 C# 中具有其进程的实例



我可以从进程中获取可执行文件的位置,如何从文件中获取图标?

也许可以使用windows api LoadIcon()。我想知道是否有.NET的方式。。。

Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);

这是一个控制台应用程序实现的示例。

using System;
using System.Drawing;         //For Icon
using System.Reflection;      //For Assembly
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Gets the icon associated with the currently executing assembly
                //(or pass a different file path and name for a different executable)
                Icon appIcon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);                
            }
            catch(ArgumentException ae) 
            {
                //handle
            }           
        }
    }
}

使用ExtractIconEx(以及此处)p/invoke。您可以从任何dll或exe中提取大小图标。Shell32.dll本身有200多个图标,这些图标对于标准的Windows应用程序非常有用。你只需要先弄清楚你想要的图标的索引是什么。

编辑:我做了快速搜索,找到了这个。索引0图标是应用程序图标。

最新更新