C # FileSystemWatcher

Source: Internet
Author: User

 

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;
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.