C# Monitor and transfer or copy the changed or created file to a new location
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Diagnostics;namespaceConsoleApplication3
{classProgram
{staticvoidMain(string[]args)
{
MonitorAndTransferFiles();
Console.ReadLine();
}staticstringdestPath=@”D:CConsoleApplication2ConsoleApplication2″;staticvoidMonitorAndTransferFiles(stringsourcePath=null)
{
sourcePath=Directory.GetCurrentDirectory();
WatchFiles(sourcePath);
}staticvoidWatchFiles(stringpath)
{
FileSystemWatcherwatcher=newFileSystemWatcher();
watcher.Path=path;
watcher.NotifyFilter=NotifyFilters.LastWrite|NotifyFilters.CreationTime;
watcher.Filter=”*.*”;
watcher.Changed+=Watcher_Changed;
watcher.Created+=Watcher_Created;
watcher.EnableRaisingEvents=true;
}privatestaticvoidWatcher_Created(objectsender,FileSystemEventArgse)
{try{
Console.WriteLine($”Created:FullPath:{e.FullPath},ChangeType:{e.ChangeType}”);
File.Copy(e.FullPath,Path.Combine(destPath,Path.GetFileName(e.FullPath)),true);
}catch{
}
}privatestaticvoidWatcher_Changed(objectsender,FileSystemEventArgse)
{try{
Console.WriteLine($”Changed:FullPath:{e.FullPath},ChangeType:{e.ChangeType}”);
File.Copy(e.FullPath,Path.Combine(destPath,Path.GetFileName(e.FullPath)),true);
}catch{
}
}
}
}
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com