Link to home
Start Free TrialLog in
Avatar of Axonites
AxonitesFlag for United States of America

asked on

Help with the FileSystemWatcher Object in .NET

Hi Experts,

I am getting some odd behaviour with the FileSystemWatcher object. Please refer to my code snippets below:
Whenever I create a new XML file in the specified directory the file name get printed twice as opposed to a single time. Tried setting a breakpoint in my eventhandler and it looks like the function is getting called twice which doesnt explain things.

 What I am trying to achieve is to read the contents of the newly created and then delete it. So if the function get called twice,it will through up a filenotfound exception the second time. Came across this oddy while testing. What am I missing in here.

static void Main(string[] args)
        {
 
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = @"C:\XmlFiles";
            watcher.Filter = "*.xml";
            watcher.Created += new FileSystemEventHandler(FileCreated);
            watcher.EnableRaisingEvents = true;
            Console.WriteLine("Started...");
            Console.ReadLine();
        }
 
        static void FileCreated(object sender, FileSystemEventArgs e)
        {
           Console.WriteLine(e.FullPath);
         }

Open in new window

Avatar of abel
abel
Flag of Netherlands image

Interesting. I just copied your code into a winforms application and I can't replicate your behavior. How are you trying to create the file? Can you try to select the file in an Explorer window an hit Ctrl-C / Ctrl-V and check if that also creates two events?

What version of .NET are you on?
ASKER CERTIFIED SOLUTION
Avatar of anyoneis
anyoneis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Axonites

ASKER

Hi Abel,

I am running dotnet 3.5, got my xml file opened in an editor. when I save it onto C:\XmlFiles using different file names the created event triggers and calls the FileCreated method which is my event handler for the same.

Now Interestingly when I try the Ctrl-C/Ctrl-V sequence the event handler gets called only once.
Oh, to answer your present question, in the first create event, you will find that FileInfo(e.FullPathName).Exists is false. But it is true on the second call.

David
Here is a console program based on yours that will help illustrate the issues.

If I re-save a pre-exsting file XMLFile2.xml then create a new one XMLFile4.xml, I get:

Started...
File Changed Event
        C:\XmlFiles\XMLFile2.xml
        Exists and is 56 bytes
File Changed Event
        C:\XmlFiles\XMLFile2.xml
        Exists and is 56 bytes
File Created Event
        C:\XmlFiles\XMLFile4.xml
        Does not exist
File Deleted Event
        C:\XmlFiles\XMLFile4.xml
        Does not exist
File Created Event
        C:\XmlFiles\XMLFile4.xml
        Exists and is 56 bytes
File Changed Event
        C:\XmlFiles\XMLFile4.xml
        Exists and is 56 bytes
File Changed Event
        C:\XmlFiles\XMLFile4.xml
        Exists and is 56 bytes




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace FileSystemWatcherTest
{
    class Program
    {
        
        static void Main(string[] args)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = @"C:\XmlFiles";
            watcher.Filter = "*.xml";
            watcher.Created += new FileSystemEventHandler(FileCreated);
            watcher.Deleted += new FileSystemEventHandler(FileDeleted);
            watcher.Changed += new FileSystemEventHandler(FileChanged);
            watcher.Renamed += new RenamedEventHandler(FileRenamed);
            watcher.EnableRaisingEvents = true;
            Console.WriteLine("Started...");
            Console.ReadLine();
        }
 
        static void DisplayStuff(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("File {0} Event", e.ChangeType);
            Console.WriteLine("\t{0}", e.FullPath);
 
            FileInfo fInfo = new FileInfo(e.FullPath);
            if (fInfo.Exists)
                Console.WriteLine("\tExists and is {0} bytes", fInfo.Length);
            else
                Console.WriteLine("\tDoes not exist");
        }
 
        static void DisplayRenameStuff(object sender, RenamedEventArgs e)
        {
            Console.WriteLine("File {0} Event", e.ChangeType);
            Console.WriteLine("\tNew Name: {0}", e.FullPath);
            Console.WriteLine("\tOld Name: {0}", e.OldFullPath);
 
            FileInfo fInfo = new FileInfo(e.FullPath);
            if (fInfo.Exists)
                Console.WriteLine("\tExists and is {0} bytes", fInfo.Length);
            else
                Console.WriteLine("\tDoes not exist");
        }
 
        static void FileCreated(object sender, FileSystemEventArgs e)
        {
            DisplayStuff(sender, e);
        }
 
        static void FileDeleted(object sender, FileSystemEventArgs e)
        {
            DisplayStuff(sender, e);
        }
        static void FileChanged(object sender, FileSystemEventArgs e)
        {
            DisplayStuff(sender, e);
        }
        static void FileRenamed(object sender, RenamedEventArgs e)
        {
            DisplayRenameStuff(sender, e);
        }
    }
}

Open in new window

Hi anyoneis,

Really appreciate your time in providing a detailed example.
Can you explain to me whats happening in one of your test cases as below:

File Created Event
        C:\XmlFiles\XMLFile4.xml
        Does not exist

I came across a similar situation, when I tried to open the file created, from  my create event handler a file not found exception was thrown.
Any reason for this behavior?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't know what the reason is for the non-existent file on the created event, but I do know that it gives you a way to distinguish between the two events.

David