main(){
string path1 = "D:\Program Files (x86)\Microsoft Office"; // 监控路径1
string path2 = "D:\Program Files (x86)\Microsoft Visual Studio 14.0\lib"; // 监控路径2
string[] pathArray = { path1, path2};
for (int i = 0; i < pathArray.Count(); i++)
{
FileSystemWatcher watch = new FileSystemWatcher();
watch.Path = pathArray[i];
watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName |
NotifyFilters.DirectoryName;
watch.Filter = "*";
watch.IncludeSubdirectories = true; // 监控子目录
watch.Created += new FileSystemEventHandler(OnChangFile);
watch.EnableRaisingEvents = true;
}
private void OnChangFile(object sender, FileSystemEventArgs e)
{
Console.WriteLine("有新文件产生!");
Console.WriteLine(e.FullPath); // 打印文件的路径
Console.WriteLine(e.Name); // 打印文件的名称
}
}