Using System. IO;
// File system watcher object.
Private FileSystemWatcher watcher;
Private delegate void UpdateWatchTextDelegate (string newText );
Public void UpdateWatchText (string newText)
{
LblWatch. Text = newText;
}
Public Form1 ()
{
InitializeComponent ();
This. watcher = new FileSystemWatcher ();
This. watcher. Deleted + = new FileSystemEventHandler (watcher_Deleted );
This. watcher. Renamed + = new RenamedEventHandler (watcher_Renamed );
This. watcher. Changed + = new FileSystemEventHandler (watcher_Changed );
This. watcher. Created + = new FileSystemEventHandler (watcher_Created );
}
Void watcher_Created (object sender, FileSystemEventArgs e)
{
// Throw new NotImplementedException ();
Try
{
StreamWriter sw = new StreamWriter ("c: \ Log.txt", true );
Sw. WriteLine ("File: {0} Created", e. FullPath );
Sw. Close ();
This. BeginInvoke (new UpdateWatchTextDelegate (UpdateWatchText), "Wrote create event to log ");
}
Catch (IOException)
{
This. BeginInvoke (new UpdateWatchTextDelegate (UpdateWatchText), "Error Writing to log ");
}
}
Void watcher_Changed (object sender, FileSystemEventArgs e)
{
// Throw new NotImplementedException ();
Try
{
StreamWriter sw = new StreamWriter ("c: \ Log.txt", true );
Sw. WriteLine ("File: {0} {1}", e. FullPath, e. ChangeType. ToString ());
Sw. Close ();
This. BeginInvoke (new UpdateWatchTextDelegate (UpdateWatchText), "Wrote change event to log ");
}
Catch (IOException)
{
This. BeginInvoke (new UpdateWatchTextDelegate (UpdateWatchText), "Error Writing to log ");
}
}
Void watcher_Renamed (object sender, RenamedEventArgs e)
{
// Throw new NotImplementedException ();
Try
{
StreamWriter sw = new StreamWriter ("c: \ Log.txt", true );
Sw. WriteLine ("File renamed from {0} to {1}", e. OldName, e. FullPath );
Sw. Close ();
This. BeginInvoke (new UpdateWatchTextDelegate (UpdateWatchText), "Wrote renamed event to log ");
}
Catch (IOException)
{
This. BeginInvoke (new UpdateWatchTextDelegate (UpdateWatchText), "Error Writing to log ");
}
}
Void watcher_Deleted (object sender, FileSystemEventArgs e)
{
// Throw new NotImplementedException ();
Try
{
StreamWriter sw = new StreamWriter ("c: \ Log.txt", true );
Sw. WriteLine ("File: {0} Deleted", e. FullPath );
Sw. Close ();
This. BeginInvoke (new UpdateWatchTextDelegate (UpdateWatchText), "Wrote delete event to log ");
}
Catch (IOException)
{
This. BeginInvoke (new UpdateWatchTextDelegate (UpdateWatchText), "Error Writing to log ");
}
}
Private void cmdBrowse_Click (object sender, EventArgs e)
{
If (FileDialog. ShowDialog ()! = DialogResult. Cancel)
{
TxtLocation. Text = FileDialog. FileName;
CmdWatch. Enabled = true;
}
}
Private void cmdWatch_Click (object sender, EventArgs e)
{
Watcher. Path = Path. GetDirectoryName (txtLocation. Text); // monitoring Path (folder)
Watcher. Filter = "*. *"; // If the filter is a file name, it indicates that the file is monitored. If the Filter is * .txt, all. txt files in the specified directory must be monitored.
Watcher. policyfilter = policyfilters. LastWrite |
Policyfilters. FileName |
Yyfilters. Size;
LblWatch. Text = "Watching" + txtLocation. Text;
// Begin watching.
Watcher. EnableRaisingEvents = true;
}