我尝试使用这个简单的测试来使用FileSystemWatcher类,但 OnFileChanged 事件没有触发
[Test]
public void CanNotifyWhenFileChanged()
{
var watcher = new XMLWatcher(_path);
watcher.StartWatching();
AddXMLNodeTofile(_path);// change the file
bool c = watcher.IsFileChanged;
Assert.IsTrue(c);
}
这是我的方法
public void StartWatching()
{
var watcher = new FileSystemWatcher
{
Path =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
IncludeSubdirectories = false,
NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName,
Filter = FilePath
};
watcher.Changed += OnFileChanged;
watcher.EnableRaisingEvents = true;
}