Simple Folder Monitor Application Using VB.Net Winforms


Posted Date:     Total Responses: 0    Posted By: Hefin Dsouza   Member Level: Gold   Points/Cash: 300   


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 notifing 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
Download the Full Source Code Below


Attachments

  • Download the Whole Project (476-28110-FolderWatcher.rar)

  • Project Feedbacks

    Author: Member Level: GoldRevenue Score: 2 out of 52 out of 5
    Hi,
    Its really nice. I would like learn such type of things.

    It will be greatful for me to send the files.

    Thanks Rakesh,
    Siva Sreenath


    Post Feedback
    You must Sign In to post a feedback.
    Next Project: MyPdfTextReader 1.0
    Previous Project: Basic Calculator 1.0 in VB.NET

    Return to Project Index

    Post New Project


    Related Projects


    Top Contributors
    Today
      Last 7 Daysmore...

      Awards & Gifts

      Online Members

      More...
       
      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India