SystemHelper
Reads icons for files and folders from the Windows shell, so that rows in a tree list view can show the same icons Explorer does.
public class SystemHelper
Remarks
The icons are looked up by file extension and by "folder", not by reading the files, so the paths
need not exist and the lookup is quick. Each call returns a new Icon that the
caller owns: put it in an ImageList once per extension rather than per row.
Examples
A file browser: the icon for each node from its extension, cached by extension.
var icons = new Dictionary<string, Image>(StringComparer.OrdinalIgnoreCase);
Image IconFor(string path)
{
string extension = Path.GetExtension(path);
if (!icons.TryGetValue(extension, out Image image))
{
using (Icon icon = SystemHelper.GetFileIcon(path, SystemHelper.EnumIconSize.Small, false))
{
image = icon.ToBitmap();
}
icons.Add(extension, image);
}
return image;
}
foreach (string file in Directory.GetFiles(folder))
{
BinaryTreeListViewNode node = treeListView.Nodes.Add(Path.GetFileName(file));
node.Image = IconFor(file);
}Members at a glance
| Methods | |
|---|---|
GetFileIcon | Returns the icon the shell shows for a file of this type, optionally with the shortcut arrow over it. |
GetFolderIcon | Returns the standard folder icon the shell uses, open or closed. |
GetFolderIcon | Returns the standard folder icon the shell uses, open or closed. |
Methods
GetFileIcon static
Returns the icon the shell shows for a file of this type, optionally with the shortcut arrow over it.
Parameters
filePath- The file's path or name. It need not exist: the icon follows the extension.
size- The small (usually 16 by 16) or the large (usually 32 by 32) shell icon.
addLinkOverlaytrueto draw the shortcut arrow over the icon.
Returns
A new icon, which the caller owns and should dispose.
Exceptions
ArgumentException- The shell returned no icon.
GetFolderIcon static
Returns the standard folder icon the shell uses, open or closed.
Parameters
size- The small (usually 16 by 16) or the large (usually 32 by 32) shell icon.
folderType- The open folder, for an expanded node, or the closed one.
Returns
A new icon, which the caller owns and should dispose.
Remarks
Calls GetFolderIcon() with no path.
GetFolderIcon static
Returns the standard folder icon the shell uses, open or closed.
Parameters
folderPath- The folder's path, or
null. The shell is asked by the folder attribute alone, so the path does not change the icon: a folder with a custom icon of its own still gets the standard one. size- The small (usually 16 by 16) or the large (usually 32 by 32) shell icon.
folderType- The open folder, for an expanded node, or the closed one.
Returns
A new icon, which the caller owns and should dispose.
Exceptions
ArgumentException- The shell returned no icon.