Imports System.Runtime.InteropServices
Public Class Form1
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public 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" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" _
(ByVal pszPath As IntPtr, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Public Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
(ByVal hwndOwner As Integer, ByVal nFolder As Integer, ByRef pidl As Integer) As Integer
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_DISPLAYNAME = &H200
Private Const SHGFI_TYPENAME = &H400
Private Const SHGFI_PIDL = &H8
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const CSIDL_DRIVES = &H11
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim nIndex As Integer = 1
Dim shinfo As SHFILEINFO
Dim arDrives(), fi As String
arDrives = IO.Directory.GetLogicalDrives()
shinfo = New SHFILEINFO()
lstIcons.SmallImageList = imgIcons
lstIcons.LargeImageList = imgIcons
Dim tmpPidl As IntPtr
SHGetSpecialFolderLocation(0, CSIDL_DRIVES, tmpPidl)
SHGetFileInfo(tmpPidl, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_PIDL + SHGFI_DISPLAYNAME + SHGFI_ICON + SHGFI_SMALLICON)
Marshal.FreeCoTaskMem(tmpPidl)
Dim myIcon As System.Drawing.Icon
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
imgIcons.Images.Add(myIcon)
lstIcons.Items.Add(shinfo.szDisplayName, 0)
For Each fi In arDrives
SHGetFileInfo(fi, FILE_ATTRIBUTE_NORMAL, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON + SHGFI_SMALLICON + SHGFI_DISPLAYNAME)
myIcon = Nothing
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
imgIcons.Images.Add(myIcon)
lstIcons.Items.Add(shinfo.szDisplayName, nIndex)
nIndex += 1
Next
End Sub
End Class