2021/12/22(Wed) 12:37:32 編集(投稿者)
※削除ファイルを考慮していなかったので処理追加
Module Module1
Private checkPath As Dictionary(Of String, Date) = New Dictionary(Of String, Date)
Public Sub ClserCheckPath()
checkPath = New Dictionary(Of String, Date)
End Sub
Public Function StartCheckPath(path As String) As Boolean
Dim IsChange As Boolean = False
Dim temp As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim di As New System.IO.DirectoryInfo(path)
Dim files As System.IO.FileInfo() = di.GetFiles("*", System.IO.SearchOption.AllDirectories)
For Each f As System.IO.FileInfo In files
Dim fullPath As String = f.FullName
f.Refresh()
If checkPath.ContainsKey(fullPath) Then
If checkPath(fullPath) <> f.LastWriteTime Then
checkPath(fullPath) = f.LastWriteTime
IsChange = True
End If
Else
checkPath.Add(fullPath, f.LastWriteTime)
IsChange = True
End If
temp.Add(fullPath, fullPath)
Next
'削除ファイル確認
Dim keys() As String = checkPath.Keys.ToArray
For Each key As String In keys
If temp.ContainsKey(key) = False Then
IsChange = True
checkPath.Remove(key)
End If
Next
Return IsChange
End Function
End Module
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Call ClserCheckPath()
'ストップウォッチを開始する
Dim sw As New System.Diagnostics.Stopwatch()
sw.Start()
Debug.Print("{0}", StartCheckPath("D:\Test\"))
'ストップウォッチを止め結果を表示する
sw.Stop()
Debug.Print("{0}", sw.Elapsed)
'一秒間(1000ミリ秒)停止するこの間に変更があるものとする
System.Threading.Thread.Sleep(1000)
sw.Start()
Debug.Print("{0}", StartCheckPath("D:\Test\"))
'ストップウォッチを止め結果を表示する
sw.Stop()
Debug.Print("{0}", sw.Elapsed)
End Sub