Private Sub Timer1_Timer()
' à»ÃÕºà·Õº Drive ¨Ò¡ DriveListBox ¡Ñº ListView µÒÁàÇÅÒ·ÕèµÑé§äÇé àªè¹ ·Ø¡æ 1000 millisecond (1 ÇÔ¹Ò·Õ)
Call RefreshDrives
End Sub
' â»Ãá¡ÃÁÂèÍÂ㹡ÒÃà»ÃÕºà·Õº Drive ¨Ò¡ DriveListBox ¡Ñº ListView
Private Sub RefreshDrives()
Dim DrvDet As Integer ' µÑÇá»Ã Drive Detect µÃǨÊͺ Drive ·Ñé§ËÁ´¨Ò¡ DriveListBox
Dim DrvChk As Integer ' Drive Check ´Ñ¡ Drive ãËÁè·Õèà¢éÒÁÒ
Dim DriveFlag As Boolean ' µÃáзÕèá¨é§ãËéÃÙéÇèÒÁÕ¡ÒÃà¾ÔèÁ ËÃ×Í ¶Í´ Removeable Drive ËÃ×ÍäÁè
' ÍèÒ¹ Drive ·Ñé§ËÁ´à¢éÒÁÒà¡çºäÇéã¹ DriveListBox ¡è͹ ... µÒÁàÇÅÒ·ÕèµÑé§äÇé
DriveDetect.Refresh
' à»ÃÕºà·Õº¨Ó¹Ç¹ DriveListBox ¡Ñº ListView
' à§×èÍ¹ä¢ ¨ÃÔ§ ... áÊ´§ÇèÒÁÕ¡ÒÃ¹Ó Removable Drive à¢éÒÁÒãËÁè
If DriveDetect.ListCount > lvwDrives.ListCount Then
' Åٻǧ¹Í¡ÊØ´ãËé¹ÑºµÒÁ¨Ó¹Ç¹ Drive ·ÕèÁÕÍÂÙè·Ñé§ËÁ´¨Ò¡ DriveListBox
For DrvDet = 0 To DriveDetect.ListCount
' Åٻǧ㹤×Í¡Ò÷´ÊͺÇèÒÁÕ Removable Drive µÑÇä˹·Õèà¢éÒÁÒãËÁè
For DrvChk = 0 To lvwDrives.ListCount
' ËÒ¡ÁÕ¤èÒà·èҡѹãËéÍÍ¡¨Ò¡Åٻ㹠(DrvChk) ä»àÅ áÊ´§ÇèÒà»ç¹ Drive ·ÕèÁÕÍÂÙèà´ÔÁ
If lvwDrives.List(DrvChk) = DriveDetect.List(DrvDet) Then
DriveFlag = False
Exit For
' ¡Ã³Õ "à·ç¨" ¨Ðà¡Ô´¢Öé¹ä´é 2 ¡Ã³Õ ¤×Í
' 1. à»ÃÕºà·Õº Drive äÁèµÃ§¡Ñ¹¡ç¨ÃÔ§ áµèÂѧäÁèËÁ´¢éÍÁÙÅ ¨ÐÅÙ»µèÍ¡è͹
' 2. à»ÃÕºà·Õº¤èÒáÅéÇäÁèµÃ§¡Ñ¹àÅ áÅÐ ËÁ´¢éÍÁÙÅ áÊ´§ÇèÒà¡Ô´¡ÒÃà¾ÔèÁ Removable Drive à¢éÒä»
' ã¹¢éÍ·Õè 2 ¨Ðà¡Ô´¡ÒÃËÅØ´¨Ò¡ÅÙ»¹Í¡ (DrvDet) ´éÇÂà§×èÍ¹ä¢ DriveFlag = ¨ÃÔ§ ´éÇÂ
Else
DriveFlag = True
End If
' ÁѹÁÕ 2 ÅÙ» ... àÅÂãÊèª×è͵ÑÇá»Ãà§×èÍ¹ä¢ (DrvChk) ÁÒµèÍ·éÒ à¾×èÍ·Õè¨Ðä´éäÁè§§ áÅÐ ...
' àÃÒàÃÕ¡ÁѹÇèÒà»ç¹ Nested Loop ... ¤×Í Ç§ÃͺµéͧÍÂÙèÀÒÂ㹢ͧÍÕ¡ÅÙ» ... ÅÙ»«é͹ÅÙ»
Next DrvChk
' ¨Ò¡¡Ã³Õ·Õè 2 ´éÒ¹º¹ ... ÃÙé·Ñ¹·ÕàÅÂÇèÒÁÕ Removable Drive à¢éÒÁÒãËÁè
If DriveFlag Then
' à¾ÔèÁÃÒ¡Òà Drive µÑÇãËÁèà¢éÒä»·Õè lvwDrives (ListView Control µÑÇ·Õè«è͹àÍÒäÇé)
lvwDrives.AddItem DriveDetect.List(DrvDet)
'Debug.Print "Found New Drive: " & DriveDetect.List(DrvDet)
' Ê觪×èÍ Drive ä»·´Êͺ¡è͹ÇèÒà»ç¹»ÃÐàÀ· Removable Drive ËÃ×ÍäÁè
' ËÒ¡ãªèãËé¹ÓÃÒ¡ÒÃä¿ÅìµèÒ§æà¢éÒÁÒã¹ ListView (¿Ñ§¤ìªÑè¹ AddToListView)
If CheckRemovable(Left$(DriveDetect.List(DrvDet), 2)) Then _
Call AddToListView(UCase$(Left$(DriveDetect.List(DrvDet), 2)))
End If
' ÁѹÁÕ 2 ÅÙ» ... àÅÂãÊèª×è͵ÑÇá»Ãà§×èÍ¹ä¢ (DrvDet) ÁÒµèÍ·éÒ à¾×èÍ·Õè¨Ðä´éäÁè§§ ...
Next DrvDet
' à§×èÍ¹ä¢ à·ç¨ ... áÊ´§ÇèÒÁÕ¡ÒÃ¹Ó Removable Drive Í͡仨ҡà¤Ã×èͧ
ElseIf DriveDetect.ListCount < lvwDrives.ListCount Then
' ·Ó§Ò¹¡ÅѺ´éÒ¹¡Ñ¹¡Ñºà§×èÍ¹ä¢ "¨ÃÔ§" ¹èФÃѺ ... ¾Õè¹éͧ
' Åٻǧ¹Í¡ÊØ´ãËé¹ÑºµÒÁ¨Ó¹Ç¹ Drive ·ÕèÁÕÍÂÙè·Ñé§ËÁ´¨Ò¡ ListView Control ·Õè«è͹àÍÒäÇé (lvwDrives)
For DrvDet = 0 To lvwDrives.ListCount
' Åٻǧ㹤×Í¡Ò÷´ÊͺÇèÒÁÕ Removable Drive µÑÇä˹·Õè¶Ù¡¶Í´ÍÍ¡ä»áÅéÇ
For DrvChk = 0 To DriveDetect.ListCount
' ËÒ¡ÁÕ¤èÒà·èҡѹãËéÍÍ¡¨Ò¡ÅÙ»ä»àÅ (áÊ´§ÇèÒà»ç¹ Drive à´ÔÁ)
' ¤Ó͸ԺÒ¨ÐàËÁ×͹¡Ñ¹¡Ñº¡ÒÃà¾ÔèÁ Removable Drive à¢éÒÁÒãËÁè
If DriveDetect.List(DrvChk) = lvwDrives.List(DrvDet) Then
DriveFlag = False
Exit For
Else
DriveFlag = True
End If
Next DrvChk
If DriveFlag Then
'Debug.Print "Drive Has Been Removed: " & lvwDrives.List(DrvDet)
' äÁèµéͧµÃǨÊͺ¡çä´éÇèÒÁѹà»ç¹ Removable Drive ËÃ×ÍäÁè ... ¶Í´ÁѹÍ͡仡ç¹Ñè¹áËÅÐÁѹà»ç¹ ... ÍÔÍÔÍÔÍÔÍÔ
'If CheckRemovable(UCase$(Left$(lvwDrives.List(DrvDet), 2))) Then _
Call RemoveFromListView(UCase$(Left$(lvwDrives.List(DrvDet), 2)))
' źÃÒ¡ÒÃÍÍ¡¨Ò¡ ListView Control (lvwViewer)
Call RemoveFromListView(UCase$(Left$(lvwDrives.List(DrvDet), 2)))
' źÃÒ¡Òà Drive ÍÍ¡¨Ò¡ ListView Control µÑÇ·Õè«è͹àÍÒäÇéã¹ lvwDrives
lvwDrives.RemoveItem DrvDet
End If
Next DrvDet
End If
End Sub
' ÁÕ Remove Drive à¾ÔèÁ¢Öé¹ÁÒ ¡çµéͧ¹ÓÃÒª×èÍä¿Åì - â¿Åà´ÍÃì ä»à¾ÔèÁã¹ ListView (lvwViewer)
Private Function AddToListView(Drive As String)
Set mDrive = FSO.GetDrive(Drive)
If mDrive.DriveType = Removable And mDrive.IsReady = True Then
For Each mFile In mDrive.RootFolder.Files
DoEvents
lvwViewer.AddItem mFile.Path
nFiles = nFiles + 1
Next
' Íѹ¹Õé¡çá¤èá¨é§ÃÒ¡Òèӹǹä¿Åì¤ÃѺ¼Á
fraViewer.Caption = " ¨Ó¹Ç¹ " & nFiles & " ä¿Åì" & " "
' ¡ÁÒ·Ñé§â¿Åà´ÍÃì´éÇÂ
For Each mFolder In mDrive.RootFolder.SubFolders
DoEvents
' áÊ´§ª×èÍâ¿Åà´ÍÃìÍÍ¡ÁÒ
lvwViewer.AddItem mFolder.Path
nFolders = nFolders + 1
Next
' Êèǹ¢Í§¡ÒùѺ¨Ó¹Ç¹â¿Åà´ÍÃì
fraViewer.Caption = fraViewer.Caption & " / ¨Ó¹Ç¹ " & nFolders & " â¿Åà´ÍÃì" & " "
End If
End Function
' ÁÕ¡Òöʹ Remove Drive ÍÍ¡ä» ¡çµéͧ¹ÓÃÒª×èÍä¿Åì - â¿Åà´ÍÃì ÍÍ¡¨Ò¡ ListView (lvwViewer)
Private Function RemoveFromListView(Drive As String)
Dim sRow As Integer
Dim CountRow As Integer
CountRow = lvwViewer.ListCount - 1
For sRow = 0 To CountRow
If UCase$(Left$(lvwViewer.List(sRow), 2)) = Drive Then
' à¾ÃÒйѺÁÒ¨Ò¡·Ò§¢ÇÒÁ×ÍÊØ´à¢éÒÁÒ 3 µÑÇÍÑ¡Éà µÑǶѴÁÒËÒ¡à»ç¹¨Ø´ (Dot) ... Áѹ¤×Íä¿Åì
If Mid$(lvwViewer.List(sRow), Len(lvwViewer.List(sRow)) - 3, 1) = "." Then
nFiles = nFiles - 1
Else
nFolders = nFolders - 1
End If
lvwViewer.RemoveItem sRow
CountRow = lvwViewer.ListCount - 1
sRow = lvwViewer.ListIndex - 1
End If
Next
fraViewer.Caption = " ¨Ó¹Ç¹ " & nFiles & " ä¿Åì" & " / ¨Ó¹Ç¹ " & nFolders & " â¿Åà´ÍÃì" & " "
End Function
' ¿Ñ§¤ìªÑè¹µÃǨÊͺÇèÒ Drive ·Õèà¢éÒÁÒ¹Ñé¹à»ç¹ Removable Drive ËÃ×ÍäÁè
' ËÒ¡ãªè ãËé¤×¹¤èÒ¡ÅѺà»ç¹ True ... äÁèãªèãËé¤×¹¤èÒ False
Public Function CheckRemovable(Drive As String) As Boolean
On Error Resume Next
Set mDrive = FSO.GetDrive(Drive)
If mDrive.DriveType = Removable And mDrive.IsReady = True Then
CheckRemovable = True
Else
CheckRemovable = False
End If
End Function
|