How do I fetch the folder icon on Windows 7 using Shell32.SHGetFileInfo

Viewed 17637

I have the following code which works on Windows XP and Vista - both 32 and 64 bit:

public static Icon GetFolderIcon(IconSize size, FolderType folderType)
{
    // Need to add size check, although errors generated at present!
    uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

    if (FolderType.Open == folderType)
    {
        flags += Shell32.SHGFI_OPENICON;
    }

    if (IconSize.Small == size)
    {
       flags += Shell32.SHGFI_SMALLICON;
    } 
    else 
    {
       flags += Shell32.SHGFI_LARGEICON;
    }

    // Get the folder icon
    var shfi = new Shell32.SHFILEINFO();
    Shell32.SHGetFileInfo(  null, 
                            Shell32.FILE_ATTRIBUTE_DIRECTORY, 
                            ref shfi, 
                            (uint) Marshal.SizeOf(shfi), 
                            flags );

    Icon.FromHandle(shfi.hIcon);    // Load the icon from an HICON handle

    // Now clone the icon, so that it can be successfully stored in an ImageList
    var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

    User32Dll.DestroyIcon( shfi.hIcon );        // Cleanup
    return icon;
}

The constants are defined the following way:

public const uint SHGFI_ICON = 0x000000100;
public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
public const uint SHGFI_OPENICON = 0x000000002;
public const uint SHGFI_SMALLICON = 0x000000001;
public const uint SHGFI_LARGEICON = 0x000000000;
public const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;

This gives the following results in windows 7 when fetching the folder icon:

alt text

While at Vista - using the same method result in the following folder icon:

alt text

I would like the "correct" Windows folder icon for Windows 7 also - not the icon used to indicate the drive where Windows is installed.

I don't know the win32 API and my non-managed programming is next to none on the Windows platform.

2 Answers
Related