Using the FileSystem Watcher Class in Winforms

Initiotech
Posted by in Windows Forms category on for Intermediate level | Views : 8678 red flag

The Article explains the usage of FileSystemWatcher class in winforms for logging Deleting,Renaming,Modifications of files.


 Download source code for Using the FileSystem Watcher Class in Winforms

Initiotech's Folder Watcher


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 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 functions.

  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.

Page copy protected against web site content infringement by Copyscape

About the Author

Initiotech
Full Name: Hefin Dsouza
Member Level: Starter
Member Status: Member
Member Since: 9/16/2009 12:32:23 PM
Country: India

http://hefin.in
My Name is Hefin Dsouza. I am a faculty at Computer Education Institute.I am also an Freelance Software Developer and Software Trainner.I am working with DotNet Technologies from the past 8 years.I am an Microsoft Certified Windows Application Developer for .NET 3.5. And also a Blogger at http://blog.hefin.in

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)