Axonites
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.
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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).E xists is false. But it is true on the second call.
David
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
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);
}
}
}
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
David
What version of .NET are you on?