Windowsフォームアプリにファイル監視を実装しようとしていますが、問題が発生しています。イベントがトリガーされるたびに、フォームがクラッシュし続けます。
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "";
FileSystemWatcher watch = new FileSystemWatcher();
watch.Path = @"C:\files\";
watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watch.Filter = "*.txt";
watch.Changed += new FileSystemEventHandler(writeTb);
watch.Created += new FileSystemEventHandler(writeTb);
watch.Deleted += new FileSystemEventHandler(writeTb);
watch.Renamed += new RenamedEventHandler(writeTb);
watch.EnableRaisingEvents = true;
}
private void writeTb(object source, FileSystemEventArgs e)
{
textBox1.Text += e.ChangeType + ": " + e.FullPath;
}