In the MSDN documentation, a console application is provided in the
FileSystemWatcher article (just search for FileSystemWatcher Class). I am
attempting to reproduce essentially the same thing with a Windows form, and
I am utterly baffled. I believe it's because I don't fully understand the
raising and handling of events. Specifically, the author provides the
following lines within the Main() method ("watcher" is the
FileSystemWatcher):
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
Where would these go in a WinForms app?
Also, these lines are provided, and my question is the same: where in my
WinForm code do they need to be placed?:
// Define the event handlers.
public static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
I feel like I've exhausted every possible scenario, but I must not have. I'm
simply trying to have a textBox report when a file has been added to a
particular directory.
I don't expect anybody to actually READ all this, but just in case they
might here is my (useless) code as it stands:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Diagnostics;
namespace FileSystemWatcher
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.IO.FileSystemWatcher fsw;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
fsw.Path = @"C:\Testing";
fsw.NotifyFilter = NotifyFilters.CreationTime;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.fsw = new System.IO.FileSystemWatcher();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.fsw)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 48);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(272, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// fsw
//
this.fsw.EnableRaisingEvents = true;
this.fsw.SynchronizingObject = this;
this.fsw.Changed += new System.IO.FileSystemEventHandler(this.fsw_Changed);
this.fsw.Created += new System.IO.FileSystemEventHandler(OnChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(208, 208);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.fsw)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void fsw_Changed(object sender, System.IO.FileSystemEventArgs e)
{
textBox1.Text = "Directory Changed";
}
public void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
textBox1.Text = "Directory Changed";
}
}
}