
Imports System.Runtime.InteropServices


Public Class FileIcons
    Private Const SHGFI_USEFILEATTRIBUTES As Short = &H10S
    Private Const SHGFI_ICON As Short = &H100S
    Private Const FILE_ATTRIBUTE_NORMAL As Short = &H80S
    Private Const FILE_ATTRIBUTE_DIRECTORY As Short = &H10
    Private Const SHGFI_TYPENAME As Short = &H400S
    Private Const SHGFI_DISPLAYNAME As Short = &H200S
    Private Const SHGFI_SMALLICON As Short = &H1S
    Private Const SHGFI_LARGEICON As Short = &H0S

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Private Structure SHFILEINFO
        Dim hIcon As IntPtr
        Dim iIcon As Integer
        Dim dwAttributes As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String
    End Structure

    Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

    ' ------------------------------------------------------------------------------------ FILE ICONS
    Public Shared Function getIcon(ByVal sFileName As String) As System.Drawing.Icon
        Return _getIcon(sFileName, SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES)
    End Function
    Public Shared Function getSmallIcon(ByVal sFileName As String) As System.Drawing.Icon
        Return _getIcon(sFileName, SHGFI_SMALLICON Or SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES)
    End Function
    Public Shared Function getLargeIcon(ByVal sFileName As String) As System.Drawing.Icon
        Return _getIcon(sFileName, SHGFI_LARGEICON Or SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES)
    End Function

    Private Shared Function _getIcon(ByVal sFileName As String, ByVal uFlags As Integer) As System.Drawing.Icon
        Dim shfi As SHFILEINFO = Nothing
        Dim oIcon As System.Drawing.Icon = Nothing
        Try
            SHGetFileInfo(sFileName, FILE_ATTRIBUTE_NORMAL, shfi, Marshal.SizeOf(shfi), uFlags)
            oIcon = System.Drawing.Icon.FromHandle(shfi.hIcon)
        Catch
        End Try
        Return oIcon
    End Function

    ' ------------------------------------------------------------------------------------ FOLDER ICONS
    Public Shared Function getFolderIcon() As System.Drawing.Icon
        Return _getFolderIcon("", SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES)
    End Function
    Public Shared Function getFolderSmallIcon() As System.Drawing.Icon
        Return _getFolderIcon("", SHGFI_SMALLICON Or SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES)
    End Function
    Public Shared Function getFolderLargeIcon() As System.Drawing.Icon
        Return _getFolderIcon("", SHGFI_LARGEICON Or SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES)
    End Function

    Private Shared Function _getFolderIcon(ByVal sFileName As String, ByVal uFlags As Integer) As System.Drawing.Icon
        Dim shfi As SHFILEINFO = Nothing
        SHGetFileInfo(sFileName, FILE_ATTRIBUTE_DIRECTORY, shfi, Marshal.SizeOf(shfi), uFlags)
        Dim oIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(shfi.hIcon)
        Return oIcon
    End Function
End Class








