Mike Pateras
Mike Pateras

Reputation: 15015

How can I get an executable's icon in .Net?

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.

Upvotes: 4

Views: 230

Answers (2)

George Johnston
George Johnston

Reputation: 32278

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

Upvotes: 3

David
David

Reputation: 25470

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);

Upvotes: 1

Related Questions