Csharp/CSharp Tutorial/File Directory Stream/FileSystemWatcher

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

FileSystemWatcher Demo

<source lang="csharp">using System; using System.IO; public class MainClass {

   public static void Main(string[] args)
   {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"c:\Test";
        watcher.NotifyFilter  = NotifyFilters.LastAccess |
                                NotifyFilters.LastWrite |
                                NotifyFilters.FileName |
                                NotifyFilters.DirectoryName;
        watcher.Filter = "*.txt";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);
        watcher.EnableRaisingEvents = true;
                    
   }
   
   public static void OnChanged(object source, FileSystemEventArgs e)
   {
          Console.WriteLine("Event Fired");
   }
   public static void OnRenamed(object source, RenamedEventArgs e)
   {
       Console.WriteLine("Event Fired");
   }

}</source>

File Watch

<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO;

   public class Form1 : Form
   {
       private FileSystemWatcher watcher;
       private delegate void UpdateWatchTextDelegate(string newText);
       public Form1()
       {
           InitializeComponent();
           this.watcher = new System.IO.FileSystemWatcher();
           this.watcher.Deleted += new System.IO.FileSystemEventHandler(this.OnDelete);
           this.watcher.Renamed += new System.IO.RenamedEventHandler(this.OnRenamed);
           this.watcher.Changed += new System.IO.FileSystemEventHandler(this.OnChanged);
           this.watcher.Created += new System.IO.FileSystemEventHandler(this.OnCreate);
           DirectoryInfo aDir = new DirectoryInfo("C:\\");
       }
       public void UpdateWatchText(string newText)
       {
           lblWatch.Text = newText;
       }
       public void OnChanged(object source, FileSystemEventArgs e)
       {
           try
           {
               StreamWriter sw = new StreamWriter("C:/Log.txt", true);
               sw.WriteLine("File: {0} {1}", e.FullPath,e.ChangeType.ToString());
               sw.Close();
               this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Wrote change event to log");
           }
           catch (IOException)
           {
               this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Error Writing to log");
           }
       }
       public void OnRenamed(object source, RenamedEventArgs e)
       {
           try
           {
               StreamWriter sw = new StreamWriter("C:/Log.txt", true);
               sw.WriteLine("File renamed from {0} to {1}", e.OldName,e.FullPath);
               sw.Close();
               this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Wrote renamed event to log");
           }
           catch (IOException)
           {
               this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Error Writing to log");
           }
       }
       public void OnDelete(object source, FileSystemEventArgs e)
       {
           try
           {
               StreamWriter sw = new StreamWriter("C:/Log.txt", true);
               sw.WriteLine("File: {0} Deleted", e.FullPath);
               sw.Close();
               this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Wrote delete event to log");
           }
           catch (IOException)
           {
               this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Error Writing to log");
           }
       }
       public void OnCreate(object source, FileSystemEventArgs e)
       {
           try
           {
               StreamWriter sw = new StreamWriter("C:/FileLogs/Log.txt", true);
               sw.WriteLine("File: {0} Created", e.FullPath);
               sw.Close();
               this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Wrote create event to log");
           }
           catch (IOException)
           {
               this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText),"Error Writing to log");
           }
       }
       private void cmdBrowse_Click(object sender, EventArgs e)
       {
           if (FileDialog.ShowDialog() != DialogResult.Cancel)
           {
               txtLocation.Text = FileDialog.FileName;
               cmdWatch.Enabled = true;
           }
       }
       private void cmdWatch_Click(object sender, EventArgs e)
       {
           watcher.Path = Path.GetDirectoryName(txtLocation.Text);
           watcher.Filter = Path.GetFileName(txtLocation.Text);
           watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
           lblWatch.Text = "Watching " + txtLocation.Text;
           watcher.EnableRaisingEvents = true;
       }
       private void InitializeComponent()
       {
           this.txtLocation = new System.Windows.Forms.TextBox();
           this.cmdBrowse = new System.Windows.Forms.Button();
           this.cmdWatch = new System.Windows.Forms.Button();
           this.lblWatch = new System.Windows.Forms.Label();
           this.FileDialog = new System.Windows.Forms.OpenFileDialog();
           this.SuspendLayout();
           // 
           // txtLocation
           // 
           this.txtLocation.Location = new System.Drawing.Point(8, 26);
           this.txtLocation.Name = "txtLocation";
           this.txtLocation.Size = new System.Drawing.Size(184, 20);
           this.txtLocation.TabIndex = 0;
           // 
           // cmdBrowse
           // 
           this.cmdBrowse.Location = new System.Drawing.Point(208, 24);
           this.cmdBrowse.Name = "cmdBrowse";
           this.cmdBrowse.Size = new System.Drawing.Size(64, 24);
           this.cmdBrowse.TabIndex = 1;
           this.cmdBrowse.Text = "Browse...";
           this.cmdBrowse.UseVisualStyleBackColor = true;
           this.cmdBrowse.Click += new System.EventHandler(this.cmdBrowse_Click);
           // 
           // cmdWatch
           // 
           this.cmdWatch.Enabled = false;
           this.cmdWatch.Location = new System.Drawing.Point(88, 56);
           this.cmdWatch.Name = "cmdWatch";
           this.cmdWatch.Size = new System.Drawing.Size(80, 32);
           this.cmdWatch.TabIndex = 2;
           this.cmdWatch.Text = "Watch!";
           this.cmdWatch.UseVisualStyleBackColor = true;
           this.cmdWatch.Click += new System.EventHandler(this.cmdWatch_Click);
           // 
           // lblWatch
           // 
           this.lblWatch.AutoSize = true;
           this.lblWatch.Location = new System.Drawing.Point(8, 104);
           this.lblWatch.Name = "lblWatch";
           this.lblWatch.Size = new System.Drawing.Size(0, 13);
           this.lblWatch.TabIndex = 3;
           // 
           // FileDialog
           // 
           this.FileDialog.FileName = "openFileDialog1";
           this.FileDialog.Filter = "All Files|*.*";
           // 
           // Form1
           // 
           this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
           this.ClientSize = new System.Drawing.Size(296, 135);
           this.Controls.Add(this.lblWatch);
           this.Controls.Add(this.cmdWatch);
           this.Controls.Add(this.cmdBrowse);
           this.Controls.Add(this.txtLocation);
           this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
           this.MaximizeBox = false;
           this.MinimizeBox = false;
           this.Name = "Form1";
           this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
           this.Text = "File Monitor";
           this.ResumeLayout(false);
           this.PerformLayout();
       }
       private System.Windows.Forms.TextBox txtLocation;
       private System.Windows.Forms.Button cmdBrowse;
       private System.Windows.Forms.Button cmdWatch;
       private System.Windows.Forms.Label lblWatch;
       private System.Windows.Forms.OpenFileDialog FileDialog;
       [STAThread]
       static void Main()
       {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
           Application.Run(new Form1());
       }
   }</source>

My Directory Watcher

<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.IO;

 class Program
 {
   static void Main(string[] args)
   {
     FileSystemWatcher watcher = new FileSystemWatcher();
     watcher.Path = "C:\\";
     watcher.NotifyFilter = NotifyFilters.LastAccess
       | NotifyFilters.LastWrite
       | NotifyFilters.FileName
       | NotifyFilters.DirectoryName;
     watcher.Filter = "*.txt";
     watcher.Changed += new FileSystemEventHandler(OnChanged);
     watcher.Created += new FileSystemEventHandler(OnChanged);
     watcher.Deleted += new FileSystemEventHandler(OnChanged);
     watcher.Renamed += new RenamedEventHandler(OnRenamed);
     watcher.EnableRaisingEvents = true;
     Console.Read();
   }
   static void OnChanged(object source, FileSystemEventArgs e)
   {
     Console.WriteLine("File: {0} {1}!", e.FullPath, e.ChangeType);
   }
   static void OnRenamed(object source, RenamedEventArgs e)
   {
     Console.WriteLine("File: {0} renamed to\n{1}", e.OldFullPath, e.FullPath);
   }
 }</source>

Use FileSystemWatcher to detect file changes

<source lang="csharp">using System; using System.IO; using System.Windows.Forms; static class MainClass {

   static void Main()
   {
       using (FileSystemWatcher watch = new FileSystemWatcher())
       {
           watch.Path = Application.StartupPath;
           watch.Filter = "*.*";
           watch.IncludeSubdirectories = true;
           watch.Created += new FileSystemEventHandler(OnCreatedOrDeleted);
           watch.Deleted += new FileSystemEventHandler(OnCreatedOrDeleted);
           watch.EnableRaisingEvents = true;
           if (File.Exists("test.bin"))
           {
               File.Delete("test.bin");
           }
           using (FileStream fs = new FileStream("test.bin", FileMode.Create))
           {
           }
       }
   }
   private static void OnCreatedOrDeleted(object sender, FileSystemEventArgs e)
   {
       Console.WriteLine("\tNOTIFICATION: " + e.FullPath + "" was " + e.ChangeType.ToString());
   }

}</source>

NOTIFICATION: C:\Java_Dev\WEB\dev\CSharp\test.bin" was Deleted

Watching for filesystem updates synchronous

<source lang="csharp">using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.IO.rupression; using System.Net; using System.Net.Mail; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; public class MainClass {

   public static void Main()
   {
       FileSystemWatcher watcher = new FileSystemWatcher(@"c:\", "*.*");
       // only rename event
       WaitForChangedResult result =watcher.WaitForChanged(WatcherChangeTypes.Changed | WatcherChangeTypes.Renamed);
       switch (result.ChangeType)
       {
           case WatcherChangeTypes.Changed:
               Console.WriteLine("{0}: File "{1}" was changed",DateTime.Now, result.Name);
               break;
           case WatcherChangeTypes.Renamed:
               Console.WriteLine("{0}: File "{1}" was renamed to "{2}"",DateTime.Now, result.OldName, result.Name);
               break;
       }
   }

}</source>