Public Enum IconSize
SmallIcon = SHGFI_SMALLICON
LargeIcon = SHGFI_LARGEICON
End Enum
<StructLayout(LayoutKind.Sequential)> _
Private Structure SHFILEINFO
' pointer to icon handle
Public hIcon As IntPtr
' icon index
Public iIcon As Integer
' not used in this example
Public dwAttributes As Integer
' file pathname--marshal this as
' an unmanaged LPSTR of MAX_SIZE
<MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> _
Public szDisplayName As String
' file type--marshal as unmanaged
' LPSTR of 80 chars
<MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Declare Auto Function SHGetFileInfo _
Lib "shell32" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer
Private Shared Function GetIcon(ByVal fsi As FileSystemInfo, ByVal anIconSize As Integer) As Icon
Dim aSHFileInfo As New SHFILEINFO()
Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)
Dim uflags As Integer = SHGFI_ICON Or SHGFI_USEFILEATTRIBUTES Or anIconSize
Try
SHGetFileInfo(fsi.FullName, fsi.Attributes, aSHFileInfo, cbFileInfo, uflags)
Return Icon.FromHandle(aSHFileInfo.hIcon)
Catch ex As Exception
Return Nothing
End Try
End Function