This project is read-only.

Classes on library

  • WinAPI (Contain all Win API Calls) NAMESPACE
    • Shell32 (All API calls for Shell32.dll) STATIC CLASS
    • Shell32Vista (All API calls for Shell32.dll) STATIC CLASS
    • User32 (All API calls for User32.dll) STATIC CLASS
    • Comctl32 (All API calls for Comctl32.dll) STATIC CLASS
    • Kernel32 (All API calls for Kernel32.dll) STATIC CLASS
    • IShellFolder INTERFACE
  • Utils (Helper functions) STATIC CLASS
  • IconReader (Read, handle icons) STATIC CLASS
  • IconManager (IconReader Wrapper with caching features, automatic create a ImageList) CLASS
  • About (Store some constants about library, author, webpage, etc...) STATIC CLASS

Using IconReader Class

Get any icon from file:
Using "OSIcon.IconReader.ExtractIconFromFile(path, isLarge);"

// To extract the file icon we simply can do:
// First param will be the path to file.
// Last param define icon size (true = Large, false = Small)
Icon icon = OSIcon.IconReader.ExtractIconFromFile("C:\\pathtofile.png", true); 

Now if we want extract an icon from a resource file like shell32.dll we can do:
Using "OSIcon.IconReader.ExtractIconFromFile(path, iconIndex);"

// 5, is the icon index
// fhandle=YmxvZzEwMTUzNkBmczYudGlzdG9yeS5jb206L2F0dGFjaC8wLzAxMDAwMDAwMDAwMC5qcGc%3D
// 5 will return Open Folder image from shell32.dll
Icon icon = OSIcon.IconReader.ExtractIconFromFile("C:\\Windows\\system32\\shell32.dll", 5);

In Windows Explorer, FileZilla, etc, we can see file type (Folder, PHP Script, Dynamic Link Library, etc), to retrive that information we need to use some API calls, so using OSIcon is quite simple:
Using "OSIcon.IconReader.GetFileIcon(pathOrExtension, IconReader.IconSize, Shell32.SHFILEINFO);"

// NOTE: filename can be an partial extension (.png), or a full path ("C:\\file.png")
// shfi, store some file information
OSIcon.WinAPI.Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
Icon icon = OSIcon.IconReader.GetFileIcon(".png", IconReader.IconSize.Large, false, ref shfi);
MessageBox.Show(string.Format("Display Name: {0}\nFile Type: {1}", shfi.szDisplayName, shfi.szTypeName));

We also can retrive all files extensions from regedit and extract thier icons:
Using "OSIcon.IconReader.GetFileTypeAndIcon();"

// Get all available extensions
// Dictionary Key = extension, value = path to icon
Dictionary<string, string> _iconList = _iconList = 
// Foreach each extension
foreach (KeyValuePair<string, string> list in _iconList)
    // Extract icon from file
    Icon icon = OSIcon.IconReader.ExtractIconFromFile(_iconList[extension], isLarge ? true : false); 

Getting all icons on a file is quite simple:
Using "ExtractIconsFromFile(path, isLarge);"

// From Sample Application see: "IconExplorer.cs" UserControl
filename = "C:\\Windows\\system32\\shell.dll";
Icon[] icons = IconReader.ExtractIconsFromFile(filename, true);
if(icons.Length == 0) return; // No icons found, return
// Loop through icons array
for (int i = 0; i < icons.Length; i++)
    // Do something here, im adding icon to an ImageList
    imageList.Images.Add(i.ToString(), icons[i]);

Using IconManager Class

Sometimes we need keep getting a icon or thier type, if you need show your computer files and you have 100 files of same type eg (.php) you can reuse icon from first php file.
  • IconProperties Class
    • IconsInfo (Store shfi information) Dictionary<IconReader.IconSize, Shell32.SHFILEINFO>
    • IconsIndex (Icons index from ImageList) Dictionary<IconReader.IconSize, int>
    • Icons (Store icons image) Dictionary<IconReader.IconSize, Icon>

// If you want create a ImageList to store collected icons use true
// Small ImageList (true/false), Large ImageList (true/false)
// new OSIcon.IconManager() == new OSIcon.IconManager(true, true)
// Initalize Class, and create small, large, extralarge, jumbo
// Last param if set true, disable ExtraLarge or Jumbo if not supported by current OS
OSIcon.IconManager iconManager = new OSIcon.IconManager(true, true, true, true, true);
// Add some special icons to be used
// Add folder images
// Add Computer Drives (DVD Drive, Floppy, Hard Disk, etc.)
// You also can add manualy, icons images to ImageList
// I use ":" because file path can't have ':' in name, so its a special mark
// ExtraLarge was instroduced on XP so if you running XP or above add ExtraLarge capabilities
if (OSIcon.Utils.IsXpOrAbove())
// Jumbo was instroduced on Vista so if you running Vista or above add Jumbo capabilities
if (OSIcon.Utils.IsVistaOrAbove())
// NOTE: iconManager.IImageList[IconReader.Size] can be assign in any control that uses ImageList

Class is initalized and ready to use, now we create a function to show my computer and path contents

void ShowMyComputer()
    // List all drives on computer
    foreach (string drive in Directory.GetLogicalDrives())
        // Always when you add a icon to list it will return IconProperties 
        // and cache it on class
        OSIcon.IconManager.IconProperties iconProp = 
                   OSIcon.iconManager.AddEx(drive, IconManager.IconSizeBoth);
	  // Do something here!, sample from OSIcon Explorer:
        ListViewItem item = new ListViewItem(
        item.ImageIndex = iconProp.IconsIndex[IconReader.IconSize.Small];
        item.SubItems.Add(""); // Size
        item.SubItems.Add(iconProp.IconsInfo.Small.szTypeName); // Type name
        item.Tag = drive;
// Next function will show path contents
public void ShowPathContents(string path)
    if (path == null) return;
    // If path is empty show my computer instead
    if (path == "") 
        Cursor = Cursors.WaitCursor;
        string[] folders = Directory.GetDirectories(path);
        string[] files = Directory.GetFiles(path);
        foreach (string folder in folders) // get all folders on a path
            DirectoryInfo dirInfo = new DirectoryInfo(folder);
            // Your Code go here!, sample from OSIcon Explorer
            ListViewItem item = new ListViewItem(Path.GetFileName(folder));
            item.Tag = folder;
            // Its a folder and its already added when we initalized the class, 
            // so we can directly get by:
            // Note: Make use of OSIcon.IconManager.FolderOpen and 
            // OSIcon.IconManager.FolderClosed constants
            IconManager.IconProperties iconProp = iconManager.IconList[OSIcon.IconManager.FolderClosed];
            // we assign ImageList icon index to Image, you can use image 
            // key instead
            item.ImageIndex = iconProp.IconsIndex[IconReader.IconSize.Small];
        foreach (string file in files)
            FileInfo fi = new FileInfo(file);
            // Your Code go here!, sample from OSIcon Explorer
            // Now its a file, and we need try add the icon to class, if that 
            // icon already exist on class, will return the icon previous added
            // otherwise add and return.
            // I perfer use by extension because its easier to cache, because
            // 100 times .php is not the same as 100 times diferentFileNames.php
            IconManager.IconProperties iconProp = iconManager.AddEx(fi.Extension, 
            ListViewItem item = new ListViewItem(fi.Name);
            item.Tag = fi.FullName;
            item.ImageIndex = iconProp.IconsIndex[IconReader.IconSize.Small];
    catch (Exception ex)
        MessageBox.Show(string.Format("Error on trying acess: {0}\n\n{1}", 
                                      path, ex.Message), 
        Cursor = Cursors.Default;

ListView only have "ImageList" (should be 16x16px) and "LargeImageList".
If we want to show "ExtraLarge" icons introduced on XP and "Jumbo" from Vista or above,
we have to change "LargeImageList" to the desired size, can do so by:

* "fileExplorerList" is our ListView
* View:
* 0 = LargeIcons
* 1 = Details
* 2 = Small Icons
* 3 = List
* 4 = Title
* End View Enumeration
* 5 = ExtraLarge
* 6 = Jumbo

private void ChangeListViewV(uint index)
    // If view is bigger than 4 means user wants ExtraLarge or Jumbo
    // To show that sizes View must be set to: View.LargeIcon
    // Also we have to change Large ImageList to the disired one
    if (index > 4)
        // we change ImageList because ListView only have ImageList and LargeImageList
        switch (index)
           case 5:
              fileExplorerList.LargeImageList = iconManager.IImageList[IconReader.IconSize.ExtraLarge];
           case 6:
              fileExplorerList.LargeImageList = iconManager.IImageList[IconReader.IconSize.Jumbo];
        fileExplorerList.View = View.LargeIcon;
     // View is under normal (small or large)
     // Get back to normal
     fileExplorerList.LargeImageList = iconManager.IImageList[IconReader.IconSize.Large];
     fileExplorerList.View = (View)index;

How you can get rid of of added icons, to do so:

// I dont need .txt icon anymore, and i will not use it from ImageList
iconManager.Remove(".txt", true);
// I dont need .wav icon anymore, but i still use it from ImageList
iconManager.Remove(".wav", false);
// IconSize Small is to small and Jumbo is too big for what i want
iconManager.Remove(".exe", IconReader.IconSize.Small | IconReader.IconSize.Jumbo, true);

You not need hardcore with IconManager class
Good example of it:

string name = ".dll";

// Bad Way:
IconProperties iconProp = iconManager.IconList[name];
if(!iconProp.IsValid(name, IconReader.IconSize.Small))
    OSIcon.WinAPI.Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
    iconManager.AddEx(name, IconReader.IconSize.Small, ref shfi);
if(!iconProp.IsValid(name, IconReader.IconSize.Jumbo))
    OSIcon.WinAPI.Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
    iconManager.AddEx(name, IconReader.IconSize.Jumbo, ref shfi);

// Good Way:
OSIcon.WinAPI.Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
IconProperties iconProp = iconManager.AddEx(name, 
                      IconReader.IconSize.Small | IconReader.IconSize.Jumbo,
                      ref shfi);

Last edited Jan 13, 2010 at 12:19 AM by sn4k3, version 4


No comments yet.