文件监听

1,可以使用FileSystemWatcher监听文件夹

var _watcher = new FileSystemWatcher();
_watcher.Path = @"D://file"; //监听的文件夹路径
_watcher.Filter = "*.txt"; //监听文件后缀
_watcher.Created += new FileSystemEventHandler((x, y) =>Console.WriteLine("Created"));
_watcher.Error += new ErrorEventHandler( (x, y) =>Console.WriteLine("Error"));
_watcher.IncludeSubdirectories = true; //是否监听子文件夹
_watcher.EnableRaisingEvents = true;

2, 但是有时候是需要监听远程文件夹,监听的文件夹可以是远程文件路径

_watcher.Path = @"\\10.31.2.221\shared\";

也可以设置远程文件夹设置映射到本地,在cmd里面设置,用户名和密码

NET USE Z:  \\10.31.2.221\shared /user:UserName Password

设置成功了之后就可以使用Z盘路径监听

_watcher.Path = @"Z:\";

你可能感兴趣的:(文件监听)