Ever noticed how Sitecore get many nice thumbnails of various file formats in the media library? And in the filebrowser?
I just had a project, where I was to list files from a network share. The list would show the filename, and for usability reasons, also a file icon, that was associated with the file.
The image shows how the resulting control would look, if pointed to a Sitecore website root.
In the old days, I would have search the net for a proper file-icon collection, which would have an icon file for each file type, I would expect to show. This method would work, but there’s a lot of code in finding the correct icon, and also maintaining the icons.
Sitecore has made their method of showing a file icon, available through the API. This means that you can get the icons of a given file, from its extension by using the following code:
// Use FileIcon.GetFileIcon to get a fileName string for .exe, in 16x16 px, with transparent background string fullFileName = Sitecore.Resources.FileIcon.GetFileIcon(".exe", 16, 16, Color.Transparent); string fileName = Path.GetFileName(fullFileName); // combine paths to get a url in the tempfolder string url = Path.Combine(Sitecore.Configuration.Settings.TempFolderPath, fileName);
The code above will get you a URL to the file icon for .exe file type, in size 16x16, with transparent background. The magic happens in Sitecore, who has a feature to generate the thumbnails.
Whenever you request an icon through the FileIcon.GetfileIcon method, Sitecore will generate the image if it doesn’t exist, and return the file path, or simply return the file path, if the file exists. If you take a look in your tempfolder, you’ll see all the file icons that have been generated in your installation. The filenames all have the format fileicon_extension_widthxheight.png.