Skip to content

Instantly share code, notes, and snippets.

@MatthewKing
Created September 10, 2013 06:10
Show Gist options
  • Save MatthewKing/6505579 to your computer and use it in GitHub Desktop.
Save MatthewKing/6505579 to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// Provides additional icon-related functionality.
/// </summary>
public static class IconEx
{
[DllImport("shell32.dll", EntryPoint = "ExtractAssociatedIcon", CharSet = CharSet.Auto)]
internal static extern IntPtr ExtractAssociatedIcon(
HandleRef hInst,
StringBuilder iconPath,
ref int index);
/// <summary>
/// Returns an icon representation of an image that is contained in the specified file.
/// This is identical to Icon.ExtractAssociatedIcon, except that it works when filePath
/// is on a network share.
/// </summary>
/// <param name="filePath">The path to the file that contains an image.</param>
/// <returns>
/// The System.Drawing.Icon representation of the image that is contained in the
/// specified file.
/// </returns>
public static Icon ExtractAssociatedIcon(string filePath)
{
int index = 0;
StringBuilder iconPath = new StringBuilder(filePath);
IntPtr handle = ExtractAssociatedIcon(
new HandleRef(null, IntPtr.Zero),
iconPath,
ref index);
if (handle != IntPtr.Zero)
{
return Icon.FromHandle(handle);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment