views:

59

answers:

2

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.

+1  A: 

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
+3  A: 

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