Friday, December 10, 2010

FileSystemWatcher

This control helps to monitor Files or directories

1. Drag a FileSystemWatcher component to the system tray.

set the following properties
Include sub directories : true
Path : d:\

we will have to write the events for created,deleted and renamed

using System.IO;
StreamWriter sw,sw1,sw2;
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
sw = File.AppendText ("e:\\testing.txt");
// MessageBox.Show(e.FullPath + " created");
sw.WriteLine(e.FullPath + " created");
sw.Close();
}
private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
sw1 = File.AppendText ("e:\\testing1.txt");
sw1.WriteLine(e.FullPath + "Deleted");
sw1.Close();
}

private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
{
sw2 = File.AppendText("e:\\testing1.txt");
sw2.WriteLine(e.FullPath+ " Renamed");
sw2.Close();
}

0 comments:

Post a Comment