Sharp-Developer.NET     Powered by ASP.NET
Current language is English
Switch to Russian
Main
  My blog
  Articles
  Take fun
  Useful links
.NET Utils
  RegexTuner .NET
  Code2Html .NET
Google
  Toolbar buttons
.NET CodeBank
  WinForms
  Helper classes
  #ManagmentConsole
ASP.NET Projects
  AdSenseASP.NET
About me
  CV / Resume

IconExtractor

This is an example how to retrieve an icon for a file class without a file.

Reference: MSDN/HOWTO: Retrieve an Icon for a File Class Without a File

Downloads

  IconExtractor.cs - C# source file
  IconExtractor.zip - C# demo soulution

Explanation

For retrieving an icon for a file class without a file we use a Shell32.dll library.

First of all we should to import a function that will return a handle to the required icon.

[DllImport("Shell32.dll")]
private static extern IntPtr SHGetFileInfo(
    string pszPath,
    uint dwFileAttributes,
    out SHFILEINFO psfi,
    uint cbfileInfo,
    SHGFI uFlags );

where SHFILEINFO is a structure that has the next definition:

[StructLayout(LayoutKind.Sequential)]
private enum SHGFI
{
    SHGFI_ICON =              0x000000100,  // get icon
    SHGFI_DISPLAYNAME =       0x000000200,  // get display name
    SHGFI_TYPENAME =          0x000000400,  // get type name
    SHGFI_ATTRIBUTES =        0x000000800,  // get attributes
    SHGFI_ICONLOCATION =      0x000001000,  // get icon location
    SHGFI_EXETYPE =           0x000002000,  // return exe type
    SHGFI_SYSICONINDEX =      0x000004000,  // get system icon index
    SHGFI_LINKOVERLAY =       0x000008000,  // put a link overlay on icon
    SHGFI_SELECTED =          0x000010000,  // show icon in selected state
    SHGFI_ATTR_SPECIFIED =    0x000020000,  // get only specified attributes
    SHGFI_LARGEICON =         0x000000000,  // get large icon
    SHGFI_SMALLICON =         0x000000001,  // get small icon
    SHGFI_OPENICON =          0x000000002,  // get open icon
    SHGFI_SHELLICONSIZE =     0x000000004,  // get shell size icon
    SHGFI_PIDL =              0x000000008,  // pszPath is a pidl
    SHGFI_USEFILEATTRIBUTES = 0x000000010   // use passed dwFileAttribute
}
Below is the implemenntation of wraaper for WINAPI for a getting icon:
// Get icon for existing file or for file type (by extension like '*.txt')
public static Icon GetIcon( string strPath, bool small, bool selected )
{
    SHFILEINFO shfi = new SHFILEINFO(true);
    
    int cbFileInfo = Marshal.SizeOf(shfi);

    // To do this, select the SHGFI_USEFILEATTRIBUTES flag
    // so that the API assumes that the file exists
    // and it will not try to look for it on disk.

    SHGFI flags = SHGFI.SHGFI_USEFILEATTRIBUTES|SHGFI.SHGFI_ICON;

    if( selected )
        flags |= SHGFI.SHGFI_OPENICON;

    if( small )
        flags |= SHGFI.SHGFI_SMALLICON;

    SHGetFileInfo( strPath,
        256,
        out shfi,
        (uint)cbFileInfo,
        flags );
    
    return Icon.FromHandle( shfi.hIcon );
}

Thanks: Oleg Axenow


ALL CODES AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Terms of Use   |   Advertising   |   Feedback
Copyright © 2003-2006 Alexander S. Trakhimenok. Designed by Webmax.ru
Code2Html
Convert your code to colorized Html.
Check this out!
Your feedback!