Page 1 of 1

Using the FileSystemWatcher class This tutorial will show how to use the FileSystemWatcher class. This

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 19 July 2009 - 09:35 AM

In this tutorial, I will be using the FileSystemWatcher to populate a listbox with the filenames of files that are added to a directory. It will also remove files from that listbox if they are deleted from directory.

So first, we will add a ListBox to the form. I named mine "lbFiles".

Next, we need to add a couple of delegates to handle adding and removing from the ListBox. Since the FileSystemWatcher is running asynchronously, we need the delegates to be able to modify UI controls.
public delegate void UpdateTextInListBox(string message);
public delegate void RemoveTextFromListBox(string message);



Next, we will add the Form_Load event to the code-behind.
private void Form1_Load(object sender, EventArgs e)
{
	// Creates a new instance of FileSystemWatcher
	FileSystemWatcher fw = new FileSystemWatcher();
	// sets the path that will be watched
	fw.Path = @"C:\Test";
   
	// wiring up the event handlers
	fw.Created += new FileSystemEventHandler(fw_Created);
	fw.Deleted += new FileSystemEventHandler(fw_Deleted);
	// allow the object to raise events
	fw.EnableRaisingEvents = true;
}



Next, we will create the event for the file creation event.
void fw_Created(object sender, FileSystemEventArgs e)
{
    lbFiles.BeginInvoke(new UpdateTextInListBox(SetText), new object[] { e.Name });
}



Next, we will create the event for the file deletion event.
void fw_Deleted(object sender, FileSystemEventArgs e)
{
    lbFiles.BeginInvoke(new RemoveTextFromListBox(RemoveText), new object[] { e.Name });
}



Now for SetText method.
void SetText(string text)
{
    lbFiles.Items.Add(text);
}



Now for RemoveText method
void RemoveText(string text)
{
    int index = lbFiles.Items.IndexOf(text);
    if (index >= 0)
        lbFiles.Items.RemoveAt(lbFiles.Items.IndexOf(text));
}



Is This A Good Question/Topic? 1
  • +

Replies To: Using the FileSystemWatcher class

#2 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Posted 22 July 2009 - 03:55 PM

Thanks, this was interesting:)
Was This Post Helpful? 0
  • +
  • -

#3 stacson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-October 10

Posted 13 October 2010 - 07:54 AM

Hi there,

I have tried your code in a Windows Application and I'm having no joy! All I get is an empty window, not even my listbox is showing...any idea where I am going wrong?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace Assess2 
{
    partial class Form1 : Form
    {
        //handles adding and removing from the ListBox
        //it allows the delegates to modify UI controls
        public delegate void UpdateTextInListBox(string message);
        public delegate void RemoveTextFromListBox(string message);

        private void Form1_Load(object sender, EventArgs e)
        {
            // Creates a new instance of FileSystemWatcher  
            FileSystemWatcher watcher = new FileSystemWatcher();

            //sets a filter to watch jpeg files
            watcher.Filter = "*.jpg";

            // sets the path that will be watched  
            watcher.Path = @"C:\\Test123";

            // wiring up the event handlers  
            watcher.Created += new FileSystemEventHandler(watcher_Created);
            watcher.Changed += new FileSystemEventHandler(watcher_Changed);
            watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);

            // allow the object to raise events  
            watcher.EnableRaisingEvents = true;
        }

        void watcher_Created(object sender, FileSystemEventArgs e)
        {
            //create the event for the file creation event.
            lblFiles.BeginInvoke(new UpdateTextInListBox(SetText), new object[] { e.Name });
        }

        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            //create the event for the file creation event.
            lblFiles.BeginInvoke(new UpdateTextInListBox(SetText), new object[] { e.Name });
        }

        void watcher_Deleted(object sender, FileSystemEventArgs e)
        {
            //create the event for the file deletion event
            lblFiles.BeginInvoke(new RemoveTextFromListBox(RemoveText), new object[] { e.Name });
        }

        void SetText(string text)
        {
            //SetText method.
            lblFiles.Items.Add(text);
        }

        void RemoveText(string text)
        {
            //RemoveText method
            int index = lblFiles.Items.IndexOf(text);
            if (index >= 0)
                lblFiles.Items.RemoveAt(lblFiles.Items.IndexOf(text));
        }
    }
}


Cheers :?:
Was This Post Helpful? 0
  • +
  • -

#4 juanma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-August 10

Posted 14 October 2010 - 02:30 AM

I try your code and work (the changed rise 2 times and when create 4 times) but if I comment that it work ok.
I needed to modify the line
watcher.Path = @"C:\\Test123";
to  
watcher.Path = @"C:\Test123";  



Also be aware that when you see the form, it not have the initial files, If you want that you need in the load event get the files in the folder.

cheers (and sorry the mistakes in writting, I'm not english speaker)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1