jwdcfdeveloper
Programmer
I am using the FileSystemWatcher class in a Winform to watch and then report changes to a list box like this:
FileSystemWatcher watcher = new FileSystemWatcher(@"C:\");
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
}
private void button1_Click(object sender, System.EventArgs e)
{
folderBrowserDialog1.ShowDialog();
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
listBox1.Items.Add(
DateTime.Now.ToShortDateString() +" "+ e.ChangeType + " " +
e.Name + " " + e.FullPath);
}
private void button2_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
}
private void button4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
I cannot get the data to display in the list box at all after I open the explorer window and select a folder to watch. I did not hook up the code yet for the checkbox that indicates a watched folder, because I'm still trying to get the FileSystemWatcher to do it's job. Does anyone have any ideas about how to get this event to work.
FileSystemWatcher watcher = new FileSystemWatcher(@"C:\");
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
}
private void button1_Click(object sender, System.EventArgs e)
{
folderBrowserDialog1.ShowDialog();
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
listBox1.Items.Add(
DateTime.Now.ToShortDateString() +" "+ e.ChangeType + " " +
e.Name + " " + e.FullPath);
}
private void button2_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
}
private void button4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
I cannot get the data to display in the list box at all after I open the explorer window and select a folder to watch. I did not hook up the code yet for the checkbox that indicates a watched folder, because I'm still trying to get the FileSystemWatcher to do it's job. Does anyone have any ideas about how to get this event to work.