Advertisement
Caminhoneiro

File change events subscribe

Jul 18th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1.     [TestClass]
  2.     public class FileSystemWatcherExample
  3.     {
  4.         [TestMethod]
  5.         public void Example()
  6.         {
  7.             var watcher = new System.IO.FileSystemWatcher();
  8.  
  9.             watcher.Path = @"c:\temp\watched";
  10.  
  11.             watcher.NotifyFilter = NotifyFilters.FileName;
  12.  
  13.             // FileName   DirectoryName  Attributes        Size
  14.             // LastWrite  LastAccess       CreationTime  Security
  15.  
  16.             watcher.Changed += FileChanged;
  17.             watcher.Created += FileChanged;
  18.             watcher.Deleted += FileChanged;
  19.             watcher.Renamed += FileRenamed;
  20.  
  21.             watcher.EnableRaisingEvents = true;
  22.  
  23.             while (true){}
  24.         }
  25.  
  26.  
  27.         void FileRenamed(object sender, RenamedEventArgs e)
  28.         {
  29.             Debug.WriteLine("------FileRenamed---------------------------");
  30.  
  31.             Debug.WriteLine(e.ChangeType);
  32.             Debug.WriteLine(e.OldName);
  33.             Debug.WriteLine(e.Name);
  34.         }  
  35.  
  36.  
  37.         void FileChanged(object sender, FileSystemEventArgs e)
  38.         {
  39.             Debug.WriteLine("------FileChanged----------------------------");
  40.  
  41.             Debug.WriteLine(e.ChangeType);
  42.             Debug.WriteLine(e.Name);
  43.         }
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement