监视文件夹的文件变更
监视文件夹的文件变更
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | using System; using System.IO; namespace ConsoleApplication1 { class Program { static void Main( string [] args) { Watch(); } static DateTime now = DateTime.Now; static string fileName = string .Empty; private static void Watch() { FileSystemWatcher myWatcher = new FileSystemWatcher( "E:\\drivers" ); myWatcher.Filter = "*.*" ; myWatcher.EnableRaisingEvents = true ; myWatcher.Changed += new FileSystemEventHandler(myWatcher_Changed); myWatcher.Created += new FileSystemEventHandler(myWatcher_Created); myWatcher.Deleted += new FileSystemEventHandler(myWatcher_Deleted); myWatcher.Renamed += new RenamedEventHandler(myWatcher_Renamed); Console.Read(); } static void myWatcher_Renamed( object sender, RenamedEventArgs e) { if ((DateTime.Now - now).TotalMilliseconds > 100 || fileName != e.Name) { Console.WriteLine( string .Format( "{0} --重命名-- {1}" , e.OldName, e.Name)); fileName = e.Name; now = DateTime.Now; } } static void myWatcher_Deleted( object sender, FileSystemEventArgs e) { if ((DateTime.Now - now).TotalMilliseconds > 100 || fileName != e.Name) { Console.WriteLine( "删除" ); fileName = e.Name; now = DateTime.Now; } } static void myWatcher_Created( object sender, FileSystemEventArgs e) { if ((DateTime.Now - now).TotalMilliseconds > 100 || fileName != e.Name) { Console.WriteLine( "创建" ); fileName = e.Name; now = DateTime.Now; } } static void myWatcher_Changed( object sender, FileSystemEventArgs e) { if ((DateTime.Now - now).TotalMilliseconds > 100 || fileName != e.Name) { Console.WriteLine( "内容改变" ); fileName = e.Name; now = DateTime.Now; } } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 如何打造一个高并发系统?