简体   繁体   中英

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

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

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

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

Use the ExtractIconEx (and here ) p/invoke. You can extract small and large icons from any dll or exe. Shell32.dll itself has over 200 icons that are quite useful for a standard Windows application. You just have to first figure out what the index is for the icon(s) you want.

Edit: I did quick SO search and found this . The index 0 icon is the application icon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM