How can I get the icon from the executable file only having an instance of it's Process in C#

前端 未结 3 1151
梦如初夏
梦如初夏 2020-12-01 05:37

I can get the executable location from the process, how do I get the icon from file?

Maybe use windows api LoadIcon(). I wonder if there is .NET way...

3条回答
  •  感情败类
    2020-12-01 05:56

    This is a sample from a console application implementation.

    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
                }           
            }
        }
    }
    

提交回复
热议问题