Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 30024 |  Welcome, Guest!   Register  Login
Home > Articles > Windows Forms > Using the FileSystem Watcher Class in Winforms

Using the FileSystem Watcher Class in Winforms

Article posted by Initiotech on 9/20/2009 | Views: 4564 | Category: Windows Forms | Level: Intermediate red flag


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

Download


 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.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Hefin Dsouza

Experience:8 year(s)
Home page:http://hefin.in
Member since:Wednesday, September 16, 2009
Level:Starter
Status: [Member]
Biography: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
>> Write Response - Respond to this post and get points
Related Posts

In this Article i am going to explain you how to Export a DataGridView(Windows) data to a TextFile(.txt)

The article will make use of the API " ShowWindow " from the user32 Dll and allow users to Minimise,Maximize and manuplate other windows from your DotNet Application

In this article, I will be explain How to create a key in the registry using C#?

In Windows application development, we have few built-in extender components like ErrorProvider, Tooltip, etc. Extender component will add few more properties to built-in controls to enhance the functionality. In this article we will discuss about creating a custom extender component for Windows controls.

Abstract: In this article, I am going to explain setup and deployment procedure in windows project by using visual studio. To demonstrate this article I took small Merlin.acs [.acs=Agent character structured storage file] COM Component and teddy Bear Database Manips in my Windows Project. Finally I will give total roundtrip for Setting up and deployment of windows Project. I divided this article into two Parts. In this part, I explained total development procedure in windows and C# by utilizing COM Components and Database Manipulations Using ms access.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:58:46 AM