[C#] C#通过FileSystemWatcher监控文件添加或者删除事件 →→→→→进入此内容的聊天室

来自 , 2021-02-17, 写在 C#, 查看 188 次.
URL http://www.code666.cn/view/cfe795a0
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4.  
  5. class MainClass {
  6.     static void Main(string[] args) {
  7.         using (FileSystemWatcher watch = new FileSystemWatcher()) {
  8.             watch.Path = Application.StartupPath;
  9.             watch.Filter = "*.*";
  10.             watch.IncludeSubdirectories = true;
  11.  
  12.             // Attach the event handler.
  13.             watch.Created += new FileSystemEventHandler(OnCreatedOrDeleted);
  14.             watch.Deleted += new FileSystemEventHandler(OnCreatedOrDeleted);
  15.             watch.EnableRaisingEvents = true;
  16.  
  17.             Console.WriteLine("Press Enter to create a  file.");
  18.             Console.ReadLine();
  19.  
  20.             if (File.Exists("test.bin")) {
  21.                 File.Delete("test.bin");
  22.             }
  23.  
  24.             // Create test.bin.
  25.             using (FileStream fs = new FileStream("test.bin", FileMode.Create)) {
  26.                 // Do something.
  27.             }
  28.  
  29.             Console.WriteLine("Press Enter to terminate the application.");
  30.             Console.ReadLine();
  31.         }
  32.     }
  33.  
  34.     private static void OnCreatedOrDeleted(object sender, FileSystemEventArgs e) {
  35.         Console.WriteLine("\tNOTIFICATION: " + e.FullPath + "' was " + e.ChangeType.ToString());
  36.         Console.WriteLine();
  37.     }
  38. }
  39. //csharp/7637

回复 "C#通过FileSystemWatcher监控文件添加或者删除事件"

这儿你可以回复上面这条便签

captcha