C#监控文件或目录的变化

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

这个功能比较常用,在Data Loader也有一个PDF Watcher的程序,以监控指定的目录是否有新加入的PDF文件(可能来自远程传输,或是从网页中下载回来),然后对它进转换,导入到文档服务器中。

public void StartMonitor(string path)
{         
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = path;
 
        watcher.NotifyFilter = NotifyFilters.FileName;
        // Only watch pdf files.
        watcher.Filter = "*.pdf";
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
}
 
// Event handler for when a  file is created in the watched folder
private void OnChanged(object source, FileSystemEventArgs e)
{
        string word = DocumentUtility.ConvertPdfToDoc(e.FullPath);
}