• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 867
  • Last Modified:

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


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,
  • 3
2 Solutions
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.
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?file=test.zip">

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

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?file=test.zip, 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

JR-PattersonAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now