【发布时间】:2009-08-21 19:56:30
【问题描述】:
我正在使用下面的代码来监听我从服务器下载并打开的文件的更改事件。但是更改事件仅在第一次保存文件时触发,然后在后续保存时文件观察器不会触发更改事件?
谁能看到发生了什么?
private FileSystemWatcher StartWatchingFile()
{
fw = new FileSystemWatcher();
fw.Path = this.directoryLocation;
fw.Filter = this.Filename;
fw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite;
// Add event handler
fw.Changed += new FileSystemEventHandler(fw_Changed);
// Open file
System.Diagnostics.Process.Start(this.CreateAbsoluteFilePath(this.Filename));
// Begin watching.
fw.EnableRaisingEvents = true;
return fw;
}
//************************
void fw_Changed(object sender, FileSystemEventArgs e)
{
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
}
编辑:StartWatchingFile() 现在返回文件监视程序,该文件监视程序保存在一个不会被垃圾收集的类中,只是为了确保我持有整个类,因为认为 fw_changed() 函数可能无法实现叫。所以整个班级现在都没有被垃圾收集。该类保存在一个 ArrayList 中,它是类的公共成员
问候,
乔恩
【问题讨论】:
-
您发布的代码没有捕捉到 Renamed 或 Deleted 事件。我认为文件没有被重命名或删除?