以前作成した特定のフォルダを監視するコードを再度お伝えします。
以下のコードは、`vbc.exe`を使って特定のフォルダを監視し、更新があった場合にアラートを表示し、更新時刻や更新者の名前を表示するプログラムのベースです。
### `vbc.exe`を使用したフォルダ監視プログラム
```vbnet
Imports System.IO
Imports System.Windows.Forms
Public Class FolderMonitorForm
Inherits Form
Private folderPathTextBox As New TextBox()
Private WithEvents okButton As New Button()
Private WithEvents stopButton As New Button()
Private WithEvents instructionsButton As New Button()
Private listBox As New ListBox()
Private folderWatcher As FileSystemWatcher
Private lastMonitoredFolder As String = ""
Private monitoring As Boolean = False
Public Sub New()
' Set up the form
Me.Text = "Folder Monitor"
Me.Size = New Drawing.Size(400, 300)
' TextBox for folder path
folderPathTextBox.Dock = DockStyle.Top
Me.Controls.Add(folderPathTextBox)
' OK button to start monitoring
okButton.Text = "OK"
okButton.Dock = DockStyle.Top
Me.Controls.Add(okButton)
' Stop button to stop monitoring
stopButton.Text = "Stop"
stopButton.Dock = DockStyle.Top
stopButton.Enabled = False
Me.Controls.Add(stopButton)
' Instructions button
instructionsButton.Text = "Instructions"
instructionsButton.Dock = DockStyle.Top
Me.Controls.Add(instructionsButton)
' ListBox to display log
listBox.Dock = DockStyle.Fill
Me.Controls.Add(listBox)
' Restore last monitored folder path
If IO.File.Exists("last_folder.txt") Then
lastMonitoredFolder = IO.File.ReadAllText("last_folder.txt")
folderPathTextBox.Text = lastMonitoredFolder
End If
End Sub
Private Sub okButton_Click(sender As Object, e As EventArgs) Handles okButton.Click
Dim folderPath As String = folderPathTextBox.Text
If Directory.Exists(folderPath) Then
StartMonitoring(folderPath)
okButton.Enabled = False
stopButton.Enabled = True
Else
MessageBox.Show("Invalid folder path!")
End If
End Sub
Private Sub stopButton_Click(sender As Object, e As EventArgs) Handles stopButton.Click
StopMonitoring()
End Sub
Private Sub instructionsButton_Click(sender As Object, e As EventArgs) Handles instructionsButton.Click
MessageBox.Show("Select a folder, click OK to start monitoring. Use Stop to end monitoring.", "Instructions")
End Sub
Private Sub StartMonitoring(folderPath As String)
folderWatcher = New FileSystemWatcher()
folderWatcher.Path = folderPath
folderWatcher.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite Or NotifyFilters.CreationTime
AddHandler folderWatcher.Changed, AddressOf OnChanged
AddHandler folderWatcher.Created, AddressOf OnChanged
AddHandler folderWatcher.Deleted, AddressOf OnChanged
AddHandler folderWatcher.Renamed, AddressOf OnRenamed
folderWatcher.EnableRaisingEvents = True
lastMonitoredFolder = folderPath
IO.File.WriteAllText("last_folder.txt", folderPath)
monitoring = True
End Sub
Private Sub StopMonitoring()
If folderWatcher IsNot Nothing Then
folderWatcher.EnableRaisingEvents = False
folderWatcher.Dispose()
End If
monitoring = False
okButton.Enabled = True
stopButton.Enabled = False
End Sub
Private Sub OnChanged(source As Object, e As FileSystemEventArgs)
Dim changeType As String = e.ChangeType.ToString()
Dim fileName As String = Path.GetFileName(e.FullPath)
listBox.Invoke(Sub()
listBox.Items.Add($"{DateTime.Now}: {changeType} - {fileName}")
listBox.TopIndex = listBox.Items.Count - 1 ' Auto-scroll to bottom
End Sub)
End Sub
Private Sub OnRenamed(source As Object, e As RenamedEventArgs)
Dim oldFileName As String = Path.GetFileName(e.OldFullPath)
Dim newFileName As String = Path.GetFileName(e.FullPath)
listBox.Invoke(Sub()
listBox.Items.Add($"{DateTime.Now}: Renamed - {oldFileName} to {newFileName}")
listBox.TopIndex = listBox.Items.Count - 1
End Sub)
End Sub
End Class
Module FolderMonitor
Sub Main()
Application.Run(New FolderMonitorForm())
End Sub
End Module
```
### プログラムの主な機能:
- フォルダのパスを入力するTextBox。
- 「OK」ボタンで監視を開始。
- 「Stop」ボタンで監視を停止。
- 「Instructions」ボタンで簡単な使用説明を表示。
- 変更が発生した場合、`ListBox`にログを表示。
- 前回監視していたフォルダのパスを保存して、次回起動時に自動的に読み込む。
### 改善したポイント:
- ログが自動的にスクロールして最後のエントリが常に表示されるように設定。
- 「使用説明」のボタンを追加して、簡単に操作方法を確認できる。
- `ListBox`にはフルパスではなくファイル名だけを表示するように変更。
このコードを実行することで、指定したフォルダの変更をリアルタイムで監視できます。また、必要に応じてさらなる機能追加や変更が可能ですので、遠慮なくお知らせください。