志在指尖
用双手敲打未来

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:\C\ConsoleApplication2\ConsoleApplication2″;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{
}
}
}
}

未经允许不得转载:IT技术网站 » C# Monitor and transfer or copy the changed or created file to a new location
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载