Click here to Skip to main content
Licence CPOL
First Posted 12 Nov 2007
Views 15,612
Downloads 43
Bookmarked 22 times

An Easy to Understand Icon Manager Completely in .NET

By | 12 Nov 2007 | Article
This article demonstrates how to extract and display icons in .NET 2.0 without resorting to the Windows API
Screenshot - IconManager.jpg

Introduction and Background

While attempting to display icons for my application launcher program, DOMercury, I was running into some brick walls. I needed a good way to manage and extract icons from files and display the correct icon for certain file types. I saw many examples using the Windows API, but I decided that since I was writing DOMercury mainly in .NET there should be a simple .NET solution. After scavenging through MSDN, I came across System.Drawing.Icon.ExtractAssociatedIcon(string filename). With this little gem, I was able to build a simple and easy to understand Icon Manager class.

Using the Code

Since I planned on sharing it with all of you, I wanted to make the IconManager class straightforward, with its main points of interest clear to detect and understand.

I wrote it as a single instance class, since there should be only one instance of the IconManager in each program. However, it does, in essence, wrap around a global dictionary of icons, so it still needs to be initialized. Therefore, the first thing you should do in your code is call:

IconManager.Initialize(); 

This basically just instantiates the inner dictionary of the IconManager.

After that, adding icons is easy:

string file = "c:\somefolders\somefile.someextension";
IconManager.AddIcon(file);

Pulling out an icon for later use is equally as easy:

Icon bmpIcon;
string bmpExtension = ".bmp";
IconManager.GetIcon(bmpExtension);

You can either pass in the file name, or just the extension of a file in order to get the icon from it.

Points of Interest

"Why" you ask, "can I not just use ExtractAssociatedIcon by myself? Why do I have to have some manager do it for me?"

Good question. I'll explain to you a few reasons why you might want to use an icon manager. If you are just going to display a few icons every once in a while that's fine, you can go straight for the extract function, but some programs, especially application launchers, need to display many icons at a time, many of them repeats of the same icon, and, since the idea is to allow the user to get where they want to go quickly, they need to be able to call up and draw these icons fast. While I have provided a small sample application showing you how to use the IconManager, take DOMercury for a spin and notice how many icons need to be displayed in a very short amount of time. If I were doing the ExtractAssociatedIcon call every time, it would take five minutes for you to do anything in DOMercury, and therefore defeat the purpose of an application launcher.

The IconManager has a double advantage in that it saves memory by only extracting one icon of each type and reusing that icon every time a request is made for that file type, and speed that the icon does not need to be extracted every time it is called.

The IconManager is also smart enough to realize that some file types, such as *.exe, *.lnk, and *.ico each have their own icon despite being the same file type, so it creates a "key" which it uses to determine whether it should extract another icon or not:

<summary> 
/// Generates a key based on the file name to use to store with the icon. 
/// A key generation function is used so that one icon which is used 
/// for multiple files of the same type (such as Word documents) will only be 
/// stored once. 
/// </summary> 
/// <param name="path">the filename from which a key is being generated</param> 
/// <returns></returns> 
private static string GetKey(string path) {
//if path is a directory, return the key as "folder" 
{
    if (Directory.Exists(path))
    {
        return "folder";
        
    }
    //If path is an existing file, return the extension as the key 
    else if (File.Exists(path))
    {
    string ext = Path.GetExtension(path).ToLower();
    //.exe, .lnk, .ico, and .icn often have individual icons 
    //even though they are the same file type 
    //Therefore they must be treated specially 
    
        if (ext == ".exe" || ext == ".lnk" || ext == ".ico" || ext == ".icn")
    //return the specific path as the key 
        {
            return Path.GetFileName(path);
    }
    else 
    {
        return ext;
    }
    }
    //If the path is no known file or folder, return itself as the key 
    else return path;
} 
}

The key generated by the previous function is used to add and retrieve icons from the IconManager.

History

The IconManager was created to handle the Icon Management of DOMercury, and continues to do so faithfully. Check out DOMercury here.

History

  • 12th November, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dominick O'Dierno

Software Developer

United States United States

Member

I live in Milwaukee where I graduated from Milwaukee School of Engineering with a B.S. in Computer Engineering. I work as a Software Engineer at Trisept Solutions, and I develop software and embedded systems in my free time (when not spending time with my wife or watching Star Trek). My major project at the moment is DOMercury, a Quicksilver-like application launcher and productivity tool, but for Windows. Its hosted at http://www.odierno.com/domercury
 
I also sell humorous programming and math apparel at http://www.cafepress.com/cetshirts
 
My AIM screen name is lllldom
 
I enjoy athletics as well, I am an avid powerlifter, I played rugby for MSOE and I compete in Judo and Brazilian Jujitsu, and run the MSOE Judo club.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralVista - Explorer PinmemberHStrix21:52 20 Nov '07  
GeneralRe: Vista - Explorer PinmemberDominick O'Dierno4:19 21 Nov '07  
GeneralRe: Vista - Explorer PinmemberHStrix6:18 21 Nov '07  
Generali am just looking for it, thx. PinmemberHuyong Zhao1:37 13 Nov '07  
Generalinsert subject here :) [modified] Pinmembersk8er_boy28720:19 12 Nov '07  
GeneralRe: insert subject here :) Pinmemberaprenot5:27 14 Nov '07  
GeneralRe: insert subject here :) PinmemberDominick O'Dierno12:01 14 Nov '07  
GeneralRe: insert subject here :) Pinmembersk8er_boy28720:56 14 Nov '07  
GeneralRe: insert subject here :) PinmemberDominick O'Dierno12:07 15 Nov '07  
GeneralRe: insert subject here :) Pinmembersk8er_boy28720:18 15 Nov '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 12 Nov 2007
Article Copyright 2007 by Dominick O'Dierno
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid