如何从可执行文件中获取图标只有C#中的Process实例

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

也许使用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应用程序非常有用。 您只需要先弄清楚所需图标的索引是什么。

编辑:我做了快速SO搜索,发现了这一点 。 索引0图标是应用程序图标。