我使用下面的 c# 代码来捕获文件系统中的所有更改(如文件重命名或创建等),并且效果很好。但是当我将路径从“D:\”更改为“\\ServerName\folder”时 - 程序停止工作。但是在 MSDN FileSystemWatcher 类描述中悲伤:“......您可以创建一个组件来监视本地计算机、网络驱动器或远程计算机上的文件......”你能帮我改进我的代码以使用网络文件夹吗? .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace consoleWatcher
{
class Program
{
static void Main(string[] args)
{
FileSystemWatcher myWatcher = new FileSystemWatcher("D:\\");
myWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
myWatcher.Changed += new FileSystemEventHandler(OnChanged);
myWatcher.Created += new FileSystemEventHandler(OnChanged);
myWatcher.Deleted += new FileSystemEventHandler(OnChanged);
myWatcher.Renamed += new RenamedEventHandler(OnRenamed);
myWatcher.IncludeSubdirectories = true;
myWatcher.EnableRaisingEvents = true;
Console.Read();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
}