Skip to content

FileSystemWatcher implementation

I have been doing a lot of research off late for one of my clients. Recently I ran into an issue where I had to play with FileSystemWatcher class. This good ol’ friend of mine is pretty nifty and except few tricks, you can go ahead with a straight forward implementation.

Here is a sample form code that I wrote :

 

1:  namespace Skynet.FSWatch
 2:  {
 3:      using System;
 4:      using System.IO;
 5:      using System.Text;
 6:      using System.Windows.Forms;
 7:   
 8:      public partial class MainForm : Form
 9:      {
10:          delegate void SetTextCallback(string value);
11:   
12:          public MainForm()
13:          {
14:              InitializeComponent();
15:          }
16:   
17:          private void WatchButton_Click(object sender, EventArgs e)
18:          {
19:              FileSystemWatcher watcher = new FileSystemWatcher(this.FolderPathTextBox.Text);
20:              watcher.EnableRaisingEvents = true;
21:              watcher.IncludeSubdirectories = true;
22:              watcher.InternalBufferSize = 16384;
23:   
24:              watcher.Created += new FileSystemEventHandler(FSChangeHandler);
25:              watcher.Deleted += new FileSystemEventHandler(FSChangeHandler);
26:              watcher.Changed += new FileSystemEventHandler(FSChangeHandler);
27:              watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
28:              watcher.Error += new ErrorEventHandler(watcher_Error);
29:          }
30:   
31:          void watcher_Renamed(object sender, RenamedEventArgs e)
32:          {
33:              StringBuilder stringBuilder = new StringBuilder();
34:              stringBuilder.Append("ChangeType : " + e.ChangeType + Environment.NewLine);
35:              stringBuilder.Append("OldFullPath : " + e.OldFullPath + Environment.NewLine);
36:              stringBuilder.Append("FullPath : " + e.FullPath + Environment.NewLine);
37:   
38:              stringBuilder.Append("OldName : " + e.OldName + Environment.NewLine);
39:              stringBuilder.Append("Name : " + e.Name + Environment.NewLine);
40:              
41:              this.SetText(stringBuilder.ToString());
42:          }
43:   
44:          void watcher_Error(object sender, ErrorEventArgs e)
45:          {
46:              SetText(e.GetException().Message + Environment.NewLine + e.GetException().StackTrace);
47:          }
48:   
49:          void FSChangeHandler(object sender, FileSystemEventArgs e)
50:          {
51:              StringBuilder stringBuilder = new StringBuilder();
52:              stringBuilder.Append("ChangeType : " + e.ChangeType + Environment.NewLine);
53:              stringBuilder.Append("FullPath : " + e.FullPath + Environment.NewLine);
54:              stringBuilder.Append("Name : " + e.Name + Environment.NewLine);
55:   
56:              this.SetText(stringBuilder.ToString());
57:          }
58:   
59:          private void SetText(string text)
60:          {
61:              if (this.OutputTextBox.InvokeRequired)
62:              {
63:                  SetTextCallback setTextDelegate = new SetTextCallback(SetText);
64:                  this.Invoke(setTextDelegate, new object[] { text });
65:              }
66:              else
67:              {
68:                  this.OutputTextBox.Text += text;
69:              }
70:          }
71:   
72:          private void FolderPathTextBox_DoubleClick(object sender, EventArgs e)
73:          {
74:              FolderBrowserDialog dialog = GetFolderBrowseDialog("Select a Folder");
75:              DialogResult result = dialog.ShowDialog();
76:              if (result == DialogResult.OK)
77:              {
78:                  this.FolderPathTextBox.Text = dialog.SelectedPath;
79:              }
80:          }
81:   
82:          /// <summary>
83:          /// Get Browse Folder Dialog
84:          /// </summary>
85:          /// <returns>Returns FolderBrowse Dialog</returns>
86:          public static FolderBrowserDialog GetFolderBrowseDialog(
87:              string description)
88:          {
89:              FolderBrowserDialog dialog = new FolderBrowserDialog();
90:              dialog.Description = description;
91:              dialog.ShowNewFolderButton = true;
92:              return dialog;
93:          }
94:      }
95:  }
96:

Leave a Reply

Your email address will not be published. Required fields are marked *