C# 同時(shí)監(jiān)控多個(gè)文件夾內(nèi)容變更
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
private static string Alarm_content; public List<FileSystemWatcher> watchers = new List<FileSystemWatcher>(); public static void OnCreated(object source, FileSystemEventArgs e) { Console.WriteLine(string.Format("{0} {1}",e.FullPath,e.ChangeType)); Alarm_content += string.Format("{0} {1}", e.FullPath, e.ChangeType); if (!string.IsNullOrEmpty(Alarm_content)) { //sand("FileUpdate", Alarm_content); //發(fā)郵件提醒 } } [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] public string PATHS = "D:\new;D:\test"; public string[] PATHS = PATHS.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); //字符串根據(jù)";"拆分為數(shù)組 public static void SumFiles(string[] PATHS) { string date = string.Format("{0:yyyyMMdd}", DateTime.Now); foreach (var PATH in PATHS) { string path = string.Format(@"{0}\{1}\Trace", PATH, date); if (!Directory.Exists(path)) { continue; } FileSystemWatcher fsw = new FileSystemWatcher(); //設(shè)置為監(jiān)視 LastWrite 和 LastAccess 時(shí)間方面的更改,以及目錄中文本文件的創(chuàng)建、刪除或重命名 fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; //fsw.BeginInit(); //初始化 fsw.Path = path; fsw.Created += new FileSystemEventHandler(OnCreated); //創(chuàng)建文件 //fsw.Deleted += new FileSystemEventHandler(OnCreated); //刪除文件 //fsw.Renamed += new RenamedEventHandler(OnRename); //重命名文件 fsw.EnableRaisingEvents = true; } Console.WriteLine("按q退出程序."); while (Console.Read() != 'q') { }; } 該文章在 2024/1/8 2:08:14 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |