語法:
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<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 Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private nIndex = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListView1.Items.Clear()
Me.ListView1.View = View.Details
Me.ListView1.SmallImageList = Me.ImageList1
Me.ListView1.Columns.Add("檔案名稱", 500, HorizontalAlignment.Left)
Me.ListView1.Columns.Add("修改日期", 160, HorizontalAlignment.Left)
Dim shinfo As SHFILEINFO
shinfo = New SHFILEINFO()
Dim spd() As String = {"桌面", "文件", "下載"}
For Each dn As String In spd
Dim bn As New ToolStripButton(dn)
Select Case (dn)
Case "桌面"
SHGetFileInfo(My.Computer.FileSystem.SpecialDirectories.Desktop, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
bn.ToolTipText = My.Computer.FileSystem.SpecialDirectories.Desktop()
Case "文件"
SHGetFileInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
bn.ToolTipText = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Case "下載"
SHGetFileInfo(My.Computer.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", False).GetValue("{374DE290-123F-4565-9164-39C4925E467B}"), 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
bn.ToolTipText = My.Computer.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", False).GetValue("{374DE290-123F-4565-9164-39C4925E467B}")
End Select
bn.Image = System.Drawing.Icon.FromHandle(shinfo.hIcon).ToBitmap
ToolStrip2.Items.Add(bn)
AddHandler bn.Click, AddressOf ToolStripButton_Click
Next
Dim drives() As String = Directory.GetLogicalDrives()
For Each drv As String In drives
Dim bn As New ToolStripButton(drv)
SHGetFileInfo(drv, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
bn.Image = System.Drawing.Icon.FromHandle(shinfo.hIcon).ToBitmap
ToolStrip2.Items.Add(bn)
AddHandler bn.Click, AddressOf ToolStripButton_Click
Next
FileSystemWatcher1.Path = My.Computer.FileSystem.CurrentDirectory
FileSystemWatcher1_Changed(sender, Nothing)
End Sub
Private Sub FileSystemWatcher1_Changed(sender As Object, e As FileSystemEventArgs) Handles FileSystemWatcher1.Changed
ImageList1.Images.Clear()
Me.ListView1.Items.Clear()
Dim hImgSmall As IntPtr 'The handle to the system image list.
Dim shinfo As SHFILEINFO
Dim nIndex As Integer = 0
shinfo = New SHFILEINFO()
Dim dir As New DirectoryInfo(FileSystemWatcher1.Path)
Dim dirs As DirectoryInfo() = dir.GetDirectories("*.*")
Dim diNext As DirectoryInfo
For Each diNext In dirs
hImgSmall = SHGetFileInfo(diNext.FullName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim myIcon As System.Drawing.Icon
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
ImageList1.Images.Add("dir", myIcon) 'Add icon to imageList.
Dim lvItem As New ListViewItem(diNext.Name, "dir")
lvItem.SubItems.Add(diNext.LastWriteTime)
Me.ListView1.Items.Add(lvItem)
Next
For Each f In dir.GetFiles("*.*")
hImgSmall = SHGetFileInfo(f.FullName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim myIcon As System.Drawing.Icon
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
ImageList1.Images.Add(f.Extension, myIcon) 'Add icon to imageList.
Dim lvItem As New ListViewItem(f.Name, f.Extension)
lvItem.SubItems.Add(f.LastWriteTime)
Me.ListView1.Items.Add(lvItem)
Next
End Sub
Private Sub ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
FileSystemWatcher1.Path = DirectCast(sender, ToolStripButton).ToolTipText
FileSystemWatcher1_Changed(sender, Nothing)
End Sub
End Class