Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I fetch the folder icon on Windows 7 using Shell32.SHGetFileInfo
    text
    copied!<p>I have the following code which works on Windows XP and Vista - both 32 and 64 bit:</p> <pre><code>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; } </code></pre> <p>The constants are defined the following way:</p> <pre><code>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; </code></pre> <p>This gives the following results in windows 7 when fetching the folder icon:</p> <p><img src="https://photos-4.dropbox.com/t/2/AADxu8bhKAW-3XvsfDNjXfOCO8sC1dabGkdhRN8ILSpRMA/12/1193207/png/32x32/3/1513688400/0/2/folder_win7.png/EM-MhAEYvtyMnwUgAigC/jz_BeJXsJ5dznj1mXicHw9oq3eYsPl9qOu2XoNbLQGY?dl=0&amp;preserve_transparency=1&amp;size=2048x1536&amp;size_mode=3" alt="alt text"></p> <p>While at Vista - using the same method result in the following folder icon:</p> <p><img src="https://photos-4.dropbox.com/t/2/AAC1IZ23R2yD_HzPFPwU4SK2s0wxqa5TG0Fr7vPhm3Y3nQ/12/1193207/png/32x32/3/1513688400/0/2/folder_vista.png/EM-MhAEYvtyMnwUgAigC/NsEVcxLhrpzyXLgXa6getlW5qO4vTwn1SUSKG43WBF8?dl=0&amp;preserve_transparency=1&amp;size=2048x1536&amp;size_mode=3" alt="alt text"></p> <p>I would like the "correct" Windows folder icon for Windows 7 also - not the icon used to indicate the drive where Windows is installed.</p> <p>I don't know the win32 API and my non-managed programming is next to none on the Windows platform.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload