HypernovaDesktop Components · docs
Binarymission
WinForms › Tree List View › SystemHelper

SystemHelper

class.NET 6 / 8 / 10.NET Framework 4.8Binarymission.WinForms.Controls.TreeControls

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
GetFileIconReturns the icon the shell shows for a file of this type, optionally with the shortcut arrow over it.
GetFolderIconReturns the standard folder icon the shell uses, open or closed.
GetFolderIconReturns the standard folder icon the shell uses, open or closed.

Methods

GetFileIcon static

public static System.Drawing.Icon GetFileIcon( string filePath, EnumIconSize size, bool addLinkOverlay)

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.
addLinkOverlay
true to 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

public static System.Drawing.Icon GetFolderIcon( EnumIconSize size, EnumFolderType folderType)

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

public static System.Drawing.Icon GetFolderIcon( string folderPath, EnumIconSize size, EnumFolderType folderType)

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.