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.
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
FolderBrowse 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.[code]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;
}
}[/code]
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.
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.
[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);
}
}[/code]
The Different event handlers of the FileSystemWatcher
are given below :-
[code]
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); } [/code]
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