4

I'm looking to get an executable's application icon in .Net. How can I do this? I'm thinking I'll have to query its resources, some how, but I'm open to any general-case solution.

Mike Pateras
  • 14,715
  • 30
  • 97
  • 137

2 Answers2

3

From a hard-path:

Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(filePath);

From a resource dll:

// Process handle
IntPtr processHandle  = System.Diagnostics.Process.GetCurrentProcess().Handle;

// DLL path
string DLLPath = Path.Combine(Environment.SystemDirectory, "shell32.dll");

// Open folder icon index
int iconIndex         = 4; 

// Extract icon
System.IntPtr oPtr = ExtractIcon(processHandle, DLLPath, iconIndex);

Icon oIcon = Icon.FromHandle(oPtr);

From:

C# Extract icons and system icons

George Johnston
  • 31,652
  • 27
  • 127
  • 172
1

The following should work. It also pulls the icon for other file types (i.e. a white sheet of paper for .txt), though it will not pull embedded thumbnails (since those are injected by shell extensions).

icon = Icon.ExtractAssociatedIcon(filename);

David
  • 24,700
  • 8
  • 63
  • 83