*************************************************
Update - 4/26/08:
IconHandler 2.0 released

**************************************************
Update - 1/20/2007:
Use the IconHandler in conjunction with the DirectoryListingModule to set up a nice-looking directory listing for your IIS website, or create a your own directory listing template.
**************************************************

Do you like file icons?  I do.  They make it pretty easy to visually digest file lists in Windows,  when you open a file system folder with explorer.

Displaying icons in your web application can also be pretty compelling (for example, to spruce up that boring directory listing page, or visually represent documents in your web app).  Wouldn't it be cool if you can write an ASP.NET control or page that can embed these icons on your custom directory listing view? 

Unfortunately, there is no easy way to get icons for files in the .NET framework, so you have to do a little interop with the Windows shell to get the icon for a file / file extension, and then figure out a way to serve it over the web as an image.  So, I wrote an ASP.NET image handler that serves the icon for an aritrary file or extension, that you can simply drop into your application to get these pretty icons for whatever purposes you need.

Here is an example:

The basic idea is:

  1. Make a request to geticon.axd?file=FILENAME
  2. Get back the icon as a PNG file that can be displayed in the browser

You can also specify the optional size querystring parameter to select between small and large icons, for example geticon.axd?file=.ppt&size=small will give you a small version of the icon above.

Download the IconHandler application that shows how to use the icon handler here. (NOTE: This download is not affiliated with Microsoft, and is not officially supported by Microsoft or myself).  This will work on any IIS version that supports ASP.NET 2.0 (IIS 5.1, IIS6, and IIS7).

Some interesting tid-bits about the handler:

  1. It uses P/Invoke interop to get the icon using the Shell32.dll's SHGetFileInfo API
  2. It supports doing lookup of both real files (in case they override the icons), or by file type (extension).
  3. It uses the CriticalHandle class to make sure the icon handles are disposed properly
  4. It provides support for caching the handle lookups, and client/proxy caching of the returned images for improved performance
  5. You can configure a number of settings for the handler in the <iconHandler> configuration section inside your web.config

In the next post, I will include the download of the spiced up directory browsing component for IIS, that uses the icon handler to provide a skinnable directory listing for IIS.  Here is an advanced preview:

Enjoy!  As soon as I get to it, and if there is interest, I will post the source code / interesting impementation details.