我FileSystemWatcher
在我的 C# 应用程序(在 Windows 上运行)上使用 a,以便在我的应用程序中更新我当前正在浏览的文件。当我浏览本地目录时效果很好。当文件被重命名、删除或添加时,我会收到通知。但是例如,当我第一次重命名网络驱动器上的文件时,FileSystemWatcher
会通知我重命名操作,然后,当我重命名同一文件或另一个文件时,FileSystemWatcher
会通知我错误:
the specified server cannot perform the requested operation
.
然后 FileSystemWatcher 不会通知我任何事情。
有时我可以在 FileSystemWatcher 不通知我之前重命名两次...
这是我的测试代码:
static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"N:\prive\defFolder";
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Created += new FileSystemEventHandler(watcher_Changed);
watcher.Deleted += new FileSystemEventHandler(watcher_Changed);
watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
watcher.Error += new ErrorEventHandler(watcher_Error);
watcher.EnableRaisingEvents = true;
Console.Read();
watcher.Dispose();
}
static void watcher_Error(object sender, ErrorEventArgs e)
{
Console.WriteLine("error : " + e.GetException().Message);
}
static void watcher_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine("rename success");
}
static void watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("change success");
}