




Table of Contents
This library can retrieve icons from extensions and files, with additional information like file type (hard drive, folder, PHP Script File, ...). The library also provides a class for CACHE icons and creates an ImageList with the added icons.
WinAPI namespace (Contains all Win API calls)
Shell32 static class (all API calls for Shell32.dll)
Shell32Vista static class (all API calls for Shell32.dll)
User32 static class (all API calls for User32.dll)
Comctl32 static class (all API calls for Comctl32.dll)
Kernel32 static class (all API calls for Kernel32.dll)
IEnumIDList interface
IShellFolder interface
Utils static class (helper functions)
IconReader static class (read, handle icons)
IconManager class (IconReader wrapper with caching features, automatic creation of ImageList)
About static class (stores some constants about the library, author, webpage, etc...)
Get any icon from a file using "OSIcon.IconReader.ExtractIconFromFile(path, isLarge);".
Icon icon = OSIcon.IconReader.ExtractIconFromFile("C:\\pathtofile.png", true);
Now, if you want to extract an icon from a resource file like shell32.dll, you can do: "OSIcon.IconReader.ExtractIconFromFile(path, iconIndex);".
Icon icon = OSIcon.IconReader.ExtractIconFromFile("C:\\Windows\\system32\\shell32.dll", 5);
In Windows Explorer, FileZilla, etc., we can see the file type (Folder, PHP Script, Dynamic Link Library, etc.). To retrieve that information, we need to use some API calls. Using OSIcon, this is quite simple: "OSIcon.IconReader.GetFileIcon(pathOrExtension, IconReader.IconSize, Shell32.SHFILEINFO);".
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 can also retrieve file extensions from regedit and extract their icons, using: "OSIcon.IconReader.GetFileTypeAndIcon();".
Dictionary<string, string> _iconList = _iconList =
OSIcon.IconReader.GetFileTypeAndIcon();
foreach (KeyValuePair<string, string> list in _iconList)
Icon icon = OSIcon.IconReader.ExtractIconFromFile(_iconList[extension],
isLarge ? true : false);
Getting all icons on a file is quite simple, using: "ExtractIconsFromFile(path, isLarge);".
filename = "C:\\Windows\\system32\\shell.dll";
Icon[] icons = IconReader.ExtractIconsFromFile(filename, true);
if(icons.Length == 0) return; for (int i = 0; i < icons.Length; i++)
{
imageList.Images.Add(i.ToString(), icons[i]);
}
Sometimes we need to display our files and have 100s of files of the same type, e.g., .php; you can reuse icons obtained from the first PHP file in that case.
IconProperties class
IconsInfo (stores icons information) Dictionary<IconReader.IconSize, Shell32.SHFILEINFO>
IconsIndex (icons index from ImageList) Dictionary<IconReader.IconSize, int>
Icons (stores icons image) Dictionary<IconReader.IconSize, Icon>
OSIcon.IconManager iconManager = new OSIcon.IconManager(true, true, true, true, true);
iconManager.AddFolder(iconManager.IconSizeAllSupported);
iconManager.AddComputerDrives(iconManager.IconSizeAllSupported);
iconManager.IImageList[IconReader.IconSize.Small].Images.Add(
":Up-icon:", Properties.Resources.Up_icon16x16);
iconManager.IImageList[IconReader.IconSize.Large].Images.Add(
":Up-icon:", Properties.Resources.Up_icon32x32);
if (OSIcon.Utils.IsXpOrAbove())
{
iconManager.IImageList[IconReader.IconSize.ExtraLarge].Images.Add(
":Up-icon:", Properties.Resources.Up_icon48x48);
}
if (OSIcon.Utils.IsVistaOrAbove())
{
iconManager.IImageList[IconReader.IconSize.Jumbo].Images.Add(
":Up-icon:", Properties.Resources.Up_icon256x256);
}
The class is initialized and ready to use; now, we create a function to show My Computer and the path contents:
void ShowMyComputer()
{
foreach (string drive in Directory.GetLogicalDrives())
{
OSIcon.IconManager.IconProperties iconProp =
OSIcon.iconManager.AddEx(drive, IconManager.IconSizeBoth);
ListViewItem item = new ListViewItem(
iconProp.IconsInfo[IconReader.IconSize.Small].szDisplayName);
item.ImageIndex = iconProp.IconsIndex[IconReader.IconSize.Small];
item.SubItems.Add(""); item.SubItems.Add(iconProp.IconsInfo.Small.szTypeName); item.Tag = drive;
}
}
public void ShowPathContents(string path)
{
if (path == null) return;
if (path == "")
{
ShowMyComputer();
return;
}
try
{
Cursor = Cursors.WaitCursor;
string[] folders = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
foreach (string folder in folders) {
DirectoryInfo dirInfo = new DirectoryInfo(folder);
ListViewItem item = new ListViewItem(Path.GetFileName(folder));
item.Tag = folder;
IconManager.IconProperties iconProp =
iconManager.IconList[OSIcon.IconManager.FolderClosed];
item.ImageIndex = iconProp.IconsIndex[IconReader.IconSize.Small];
item.SubItems.Add("");
item.SubItems.Add(iconProp.IconsInfo[IconReader.IconSize.Small].szTypeName);
item.SubItems.Add(dirInfo.LastWriteTime.ToString());
fileExplorerList.Items.Add(item);
}
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
IconManager.IconProperties iconProp = iconManager.AddEx(fi.Extension,
IconManager.IconSizeAllSupported);
ListViewItem item = new ListViewItem(fi.Name);
item.Tag = fi.FullName;
item.ImageIndex = iconProp.IconsIndex[IconReader.IconSize.Small];
item.SubItems.Add(fi.Length.ToString());
item.SubItems.Add(iconProp.IconsInfo[IconReader.IconSize.Small].szTypeName);
item.SubItems.Add(fi.LastWriteTime.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error on trying acess: {0}\n\n{1}",
path, ex.Message),
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
Cursor = Cursors.Default;
}
}
ListView only has an "ImageList" (should be 16 x 16px) and "LargeImageList". If you want to show "ExtraLarge" icons introduced in XP, and "Jumbo" from Vista or above, you have to change "LargeImageList" to the desired size, like:
private void ChangeListViewV(uint index)
{
if (index > 4)
{
switch (index)
{
case 5:
fileExplorerList.LargeImageList =
iconManager.IImageList[IconReader.IconSize.ExtraLarge];
break;
case 6:
fileExplorerList.LargeImageList =
iconManager.IImageList[IconReader.IconSize.Jumbo];
break;
}
fileExplorerList.View = View.LargeIcon;
return;
}
fileExplorerList.LargeImageList = iconManager.IImageList[IconReader.IconSize.Large];
fileExplorerList.View = (View)index;
}
How can you get rid of of the added icons? It can be done like so:
iconManager.Remove(".txt", true);
iconManager.Remove(".wav", false);
iconManager.Remove(".exe", IconReader.IconSize.Small |
IconReader.IconSize.Jumbo, true);
You need not hard-code with the IconManager class. Here is an example:
string name = ".dll";
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);
}
OSIcon.WinAPI.Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
IconProperties iconProp = iconManager.AddEx(name,
IconReader.IconSize.Small | IconReader.IconSize.Jumbo, ref shfi);
V2.0
- Added
About class
- Added better commentaries
- Added more samples
- Updated sample application to use new icon sizes
- Updated sample application to use the new library
IconProperties class
- Added the "
Remove" function, to remove an icon from a specified size
- Supports multi sizes flags (
IconReader.IconSize.Small | IconReader.IconSize.Large)
- Returns a
Dictionary<IconReader.IconSize, int> with removed icons, where int is the icon index on the ImageList
- Added three "
IsValid" functions
bool IsValidEx(IconReader.IconSize size), same as "IsValid(IconReader.IconSize size)", but also checks if the icon is not NULL
bool IsValid(), checks if that instance contains an icon
bool IsValid(IconReader.IconSize size), checks if an icon of a specified size exists in that instance
- Changed "
IconsInfo" type from struct to Dictionary<IconReader.IconSize, Shell32.SHFILEINFO>
- Changed "
IconsIndex" type from struct to Dictionary<IconReader.IconSize, int>
- Changed "
Icons" type from struct to Dictionary<IconReader.IconSize, Icon>
- Implemented a "
Tag" object
- Disposable class
- "
IconProperties" is now a class, was a struct before
IconManager class
- Fixed the "
AddEx" function to allow adding more icons of different sizes
- Added commentaries
- Added two "
Remove" functions, allows removing icons from the cache and from the ImageList
bool Remove(string path, bool removeIconFromList), removes all icons
bool Remove(string path, IconReader.IconSize iconSize, bool removeIconFromList), removes icons of a specified size only
- Added private "
Add" function, common actions when adding icons to list (to remove redundancy)
- Added "
IsValidEx" function, same as "IsValid", but returns the matched "IconProperties"; otherwise returns a new instance
- Added two new constructors
public IconManager(bool createSmallIconList, bool createLargeIconList, bool createExtraLargeIconList, bool createJumboIconList)
public IconManager(bool createSmallIconList, bool createLargeIconList, bool createExtraLargeIconList, bool createJumboIconList, bool optimizeToOS)
- Replaced "
ImageListSmall" and "ImageListLarge" ImageLists with "IImageList" Dictionary<IconReader.IconSize, ImageList>
- Added "
IconSizeAllSupported" readeonly variable, contains all icon sizes supported by the current OS
- Added "
IconManager.IconSizeAll" constant, contains all icon sizes (Small | Large | ExtraLarge | Jumbo)
IconReader class
- Changed all functions that contain a "
IconReader.IconSize" to support new icon sizes (ExtraLarge, Jumbo)
- Added the "
ExtractIconFromResource" function, extracts an icon by name from the assembly
- Added the "
ExtractIconsFromFile" function, extracts all icons from a file; returns "Icon[]"
- Added the "
ExtractIconFromFileEx" function, identical to ExtractIconFromFile, but supports bigger sizes and icon information
- Added two new icon sizes to "
IconReader.IconSize"
IconReader.Iconsize.ExtraLarge (48x48 px.), XP or above supported
IconReader.Iconsize.Jumbo (256x256 px.), Vista or above supported
- Renamed function "
ExtractIcon" to "ExtractIconFromFile"
About class
- Added the "
ProjectAuthor" constant, my name
- Added the "
ProjecWWW" constant, this page URL
V1.0.01
- Modified to correct egregious formatting and spelling errors - JSOP - 01/03/2010
V1.0