public static class IconHelper
{
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObjetc);
[DllImport("shell32")]
private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint flags);
private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
private const uint SHGFI_ICON = 0x000000100;
private const uint SHGFI_SMALLICON = 0x000000001;
private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
public static Icon GetIconOfPath(string path, bool isSmallIcon, bool isDirectoryOrDrive)
{
uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
if (isSmallIcon)
flags |= SHGFI_SMALLICON;
uint attributes = FILE_ATTRIBUTE_NORMAL;
if (isDirectoryOrDrive)
attributes |= FILE_ATTRIBUTE_DIRECTORY;
int success = SHGetFileInfo(path, attributes, out SHFILEINFO shfi, (uint)Marshal.SizeOf(typeof(SHFILEINFO)), flags);
if (success == 0)
return null;
return Icon.FromHandle(shfi.hIcon);
}
}
path is just the path of the file/folder/drive you're getting,
isSmallIcon is self explanatory
isDirectoryOrDrive is if the path is not a file but a folder or drive