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

Hai*_*der 27 c#

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

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

The*_*edi 40

Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);
Run Code Online (Sandbox Code Playgroud)

  • `Process.MainModule`可以在系统进程或32位和64位进程之间抛出`Win32Exception`.有关详细信息,请参阅[此问题](http://stackoverflow.com/questions/9501771/how-to-avoid-a-win32-exception-when-accessing-process-mainmodule-filename-in-c). (2认同)

Rob*_*obS 13

这是来自控制台应用程序实现的示例.

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