.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Using The FileSystemWatcher Class Practically

Posted By:Hefin Dsouza       Posted Date: April 28, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

Initiotech's Folder Watcher

Introduction : Initiotech's Folder Watcher is a Folder monitoring application that will keep on monitoring changes made to a Folder.If
any changes are made to the folder their will be a MessageBox notifying about the Change.

Technical Information :It is made using the FileSystemWatcher component.

Source Code Information :The folder watcher application is a very simple to develop application.
Below I have explained some of the important code that will be useful for you to understand the working of this application.

I have written Two Methods which do most of the notification
works.

  1. ShowMessageBox:-This method will show the MessageBox for any changes made in the folder.The code given shows the declaration of the method "ShowMessageBox"


public void ShowMessageBox(string message, string caption)
{
if(showmess==true)
{
MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

   2.SaveChangesToFile :- This method will write the changes made to a File named "InitiotechsFolderWatcher.content" created in the C:\ drive.The code below shows the declaration of the SaveChangesToFile method


public void SaveChangesToFile(string content)
{
if (savefile == true)
{
fileSaver.WriteLine(content);
fileSaver.Flush();
}
}

I have declared two Boolean Variables to check whether the user has selected the Options to Show Notifications and to Save Changes to File.


bool savefile, showmess;
The Browse Button That is there Next to the TextBox shows a FolderBrowser Dialog which allows user to Choose the Folder to be Watched ,Once the user chooses the Folder the Path of the Folder gets displayed in the TextBox.


private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Choose a Folder to Watch";
if (fbd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fbd.SelectedPath;
}
}

Now that the Path has been selected you can now click the "Start Watching"button.Before that you can check the options that you want to select.
1) Hide and Watch :- Will hide the form and a icon will be placed in the Notify Area in the Task Bar. if you want to show the form again just double click the icon.
2) Notify Me Of Changes :- Will show message boxes when changes occur in
the Folder.
3)Do Not Save Changes :-Will not save changes to the File.

Now for the Start Watching Button :
-The Start watching button has the following code.


private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
savefile = true;
showmess = false;
if (checkBox1.Checked == true)
{
showmess = true;
}
if (checkBox2.Checked == true)
{
savefile= false;
}
if (checkBox3.Checked == true)
{
MessageBox.Show("Form Will Now Be Hidden in the Notify Area in the TaskBar","Form is Going to Be Hidden");
this.WindowState = FormWindowState.Minimized;
this.Visible=false;
}
folderWatch.Path = textBox1.Text;
folderWatch.Created += new FileSystemEventHandler(folderWatch_Created);
folderWatch.Renamed += new RenamedEventHandler(folderWatch_Renamed);
folderWatch.Deleted += new FileSystemEventHandler(folderWatch_Deleted);
notifyIcon1.ShowBalloonTip(20, "Initiotech's Folder Watcher is Hidden Here", "Double Click To Show Options Again", ToolTipIcon.Info);
}
}

The Different event handlers of the FileSystemWatcher are given below :- 


void folderWatch_Deleted(object sender, FileSystemEventArgs e)
{
string contents = "A File '" + e.Name + "' in '" + folderWatch.Path + "' has been Deleted at " + DateTime.Now.ToString("ddd , dd-MMM-yyyy") + " " + DateTime.Now.ToShortTimeString();
this.Visible = true;
ShowMessageBox(contents, "File Renamed");
this.Visible = false;
SaveChangesToFile(contents);
}

void folderWatch_Renamed(object sender, RenamedEventArgs e)
{
string contents = "A File '" + e.OldName +"' in '" + folderWatch.Path + "' has been Renamed to '"+e.Name + "' at " + DateTime.Now.ToString("ddd , dd-MMM-yyyy") + " " + DateTime.Now.ToShortTimeString();
this.Visible = true;
ShowMessageBox(contents, "File Renamed");
this.Visible = false;
SaveChangesToFile(contents);
}

void folderWatch_Created(object sender, FileSystemEventArgs e)
{
string contents = "A New File " + e.Name + " has been created in " + folderWatch.Path + " at " + DateTime.Now.ToString("ddd , dd-MMM-yyyy") + " " + DateTime.Now.ToShortTimeString();
this.Visible = true;
ShowMessageBox(contents, "New File Created");
this.Visible = false;
SaveChangesToFile(contents);
}

 Conclusion :

The FileSystemWatcher is a component that can watch any folder for changes made in the Folder like File or Folder Created,Deleted,Renamed and Changed.


Regards Hefin Dsouza

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend