c# FileSystemWatcher组件问题

来源:百度知道 编辑:UC知道 时间:2024/05/04 15:08:32
怎样监视文件夹?
教程只给出了下面的代码
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
label1.Text = "新建文件路径" + e.FullPath;
}
private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
label2.Text = "删除文件路径" + e.FullPath;
}

FileSystemWatcher属性path设为E:\

可运行时只显示“请在所监控的目录中操作文件”
而label1和label2没显示路径
或者举个例子说明下FileSystemWatcher组件的用法

// For example, it would be a class variable in a form class.
System.IO.FileSystemWatcher MyWatcher = new System.IO.FileSystemWatcher();
// This code would go in one of the initialization methods of the class.
MyWatcher.Path = "c:\\";
// Watch only for changes to *.txt files.
MyWatcher.Filter = "*.txt";
MyWatcher.IncludeSubdirectories = false;
// Enable the component to begin watching for changes.
MyWatcher.EnableRaisingEvents = true;
// Filter for Last Write changes.
MyWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
// Example of watching more than one type of change.
MyWatcher.NotifyFilter =
System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.Size;