Retrieving and Displaying File Icons within an ASP.NET C# Web Application

Posted on 2009-02-16
Last Modified: 2013-11-07

I am currently playing around with file management within web applications using ASP.NET 3.5. I have been searching on-line and I found a few people asking this same question, some threads were never solved and others were solved but I don't currently have the knowledge to fully understand the solution.

What I want to do (probably obvious from the title) is show file icons next to a list of files that are in a given directory. I've got the list of files, that's all easy-peasy, but I don't even know where to start with getting the icons.
I know I could just point at an image determined by the MIME type or file extension, but it's not perfect.

I have seen examples of the SHGetFileInfo function on a few sites, including MSDN and the Microsoft Support site, but nothing within a web application. Is there a way of using this, or is this for windows form applications only?

This is probably a bit out of my depth at the moment, but I am pretty persistent when I don't know how to do something so anyone's help would be greatly appreciated. Also, (if you don't mind) if you have a solution, could you break it down and explain what's happening? I hate to cut and paste code without knowing exactly what's going on!

Many Thanks,
Question by:JR-Patterson
    LVL 15

    Expert Comment

    The problem with the standard GetIcon function is that it gets an ICO formatted file.  IE does not display ICO files, they have to be converted to a web visible format, like jpeg, png, gif, bmp, etc.
    LVL 15

    Assisted Solution

    Ok, I started playing around with this, and I came up with this.  It may not work exactly what you need it for, but it's a start.  Caveats include, the file has to exist on the server, and it shows the associated program icon. So if it's a PDF, then Adobe Acrobat needs to be installed on the server, or there will be no associated icon.

    I created a Handler file, which I named geticon.ashx

    To get the icon of the specific file, you would set an img src like so <img src="geticon.ashx?">

    And here is the code for my handler.
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
       context.Response.ContentType = "image/png"
       Dim fName As String = context.Server.MapPath("~/" & context.Request.QueryString("file"))
       Dim ico As Drawing.Icon = Drawing.Icon.ExtractAssociatedIcon(fName)
       ico.ToBitmap.Save(context.Response.OutputStream, Drawing.Imaging.ImageFormat.Png)
    End Sub

    Open in new window

    LVL 15

    Accepted Solution

    You could also create a similar handler without using ExtractAssociatedIcon. Just extract the extension from the file passed in, then display the appropriate image file.  You would need to have a folder full of icon images though..

    If you request geticon.ashx?, it would return ~/icons/zip.png

    context.Response.ContentType = "image/png"
    Dim ext As String = IO.Path.GetExtension(context.Request.QueryString("file"))
    context.Response.WriteFile(context.Server.MapPath("~/icons/" & ext.TrimStart(".") & ".png"))

    Open in new window


    Author Comment

    Thanks for your responses.

    I totally overlooked the fact that I would have to have all the appropriate applications installed on the server so the server has the file/file icon associations available to use. - Rookie mistake.
    Thanks for your time and code anyway, I might try this out later just for curiosities sake.

    As for the possible solution in your third comment (23655706), this is pretty much what I have already but my code returns a specified 'unknown' icon if the handler cannot find an image for the extension of the file passed in.

    I think I'll stick with what I've got, it's just a matter of finding loads of icon images for the most commonly used files. - Surprisingly hard to find.

    Thanks again for your input.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now