T
Troy Murphy
How do I prevent the FileSystemWatcher event to keep firing while the file
is being created? When copying a file to the watched folder, the event
fires a dozen or more times! Also, the documentation states that if more
than one file is copied to the folder at the same time, a cache will queue
them up for processing, but when I drop 3 files into this folder, only the
1st (sometimes the first two) cause the event to fire.
Thanks,
Troy
Here is the problamatic Code Fragment:
protected void ListenerMethod()
{
try
{
// Setup the Watcher
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Path=@"C:\WatchedFolder";
Watcher.Filter="*.bbf";
Watcher.NotifyFilter=System.IO.NotifyFilters.LastAccess |
System.IO.NotifyFilters.LastWrite;
// Add event handlers.
Watcher.Changed += new FileSystemEventHandler(OnChanged);
Watcher.Created += new FileSystemEventHandler(OnChanged);
Watcher.Deleted += new FileSystemEventHandler(OnChanged);
Watcher.Renamed += new RenamedEventHandler(OnRename);
// Begin watching file events
this.Log("Starting File Watcher service for '"+Watcher.Filter+"' on folder:
"+Watcher.Path.ToString());
Watcher.EnableRaisingEvents=true;
}
catch(Exception ex)
{
this.Log("Could not Start the Listener.
"+ex.Message,System.Diagnostics.EventLogEntryType.Error);
}
}
protected void OnRename(object source, RenamedEventArgs e)
{
this.Log("File Renamed from: "+e.OldName.ToString()+" to:
"+e.Name.ToString());
this.OnChanged(source, (FileSystemEventArgs)e);
}
protected void OnChanged(object source, FileSystemEventArgs e)
{
//Handles the event fired by the watcher
string FileName = e.FullPath;
System.IO.FileInfo oFile = new FileInfo(FileName);
string Action = e.ChangeType.ToString();
this.Log("Received FileWatcher "+Action+" event for file: "+FileName+"
("+oFile.Length.ToString()+" bytes)");
if (Action=="Changed")
{
try
{
this.Log("Sending file: "+FileName+" to Fox BackupHandler dll");
if (this.Process(FileName))
this.Log("Backup Event Handler reported Success");
else
this.Log("Backup Event Handler Reported: "+this.LASTSTATUS.ToString()+".
Last Error =
"+this.LASTERROR.ToString(),System.Diagnostics.EventLogEntryType.Warning);
}
catch(Exception ex)
{
this.Log(ex.Message+". Backup Event Handler Reported:
"+this.LASTSTATUS.ToString()+". Last Error =
"+thisl.LASTERROR.ToString(),System.Diagnostics.EventLogEntryType.Error);
}
}
}
is being created? When copying a file to the watched folder, the event
fires a dozen or more times! Also, the documentation states that if more
than one file is copied to the folder at the same time, a cache will queue
them up for processing, but when I drop 3 files into this folder, only the
1st (sometimes the first two) cause the event to fire.
Thanks,
Troy
Here is the problamatic Code Fragment:
protected void ListenerMethod()
{
try
{
// Setup the Watcher
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Path=@"C:\WatchedFolder";
Watcher.Filter="*.bbf";
Watcher.NotifyFilter=System.IO.NotifyFilters.LastAccess |
System.IO.NotifyFilters.LastWrite;
// Add event handlers.
Watcher.Changed += new FileSystemEventHandler(OnChanged);
Watcher.Created += new FileSystemEventHandler(OnChanged);
Watcher.Deleted += new FileSystemEventHandler(OnChanged);
Watcher.Renamed += new RenamedEventHandler(OnRename);
// Begin watching file events
this.Log("Starting File Watcher service for '"+Watcher.Filter+"' on folder:
"+Watcher.Path.ToString());
Watcher.EnableRaisingEvents=true;
}
catch(Exception ex)
{
this.Log("Could not Start the Listener.
"+ex.Message,System.Diagnostics.EventLogEntryType.Error);
}
}
protected void OnRename(object source, RenamedEventArgs e)
{
this.Log("File Renamed from: "+e.OldName.ToString()+" to:
"+e.Name.ToString());
this.OnChanged(source, (FileSystemEventArgs)e);
}
protected void OnChanged(object source, FileSystemEventArgs e)
{
//Handles the event fired by the watcher
string FileName = e.FullPath;
System.IO.FileInfo oFile = new FileInfo(FileName);
string Action = e.ChangeType.ToString();
this.Log("Received FileWatcher "+Action+" event for file: "+FileName+"
("+oFile.Length.ToString()+" bytes)");
if (Action=="Changed")
{
try
{
this.Log("Sending file: "+FileName+" to Fox BackupHandler dll");
if (this.Process(FileName))
this.Log("Backup Event Handler reported Success");
else
this.Log("Backup Event Handler Reported: "+this.LASTSTATUS.ToString()+".
Last Error =
"+this.LASTERROR.ToString(),System.Diagnostics.EventLogEntryType.Warning);
}
catch(Exception ex)
{
this.Log(ex.Message+". Backup Event Handler Reported:
"+this.LASTSTATUS.ToString()+". Last Error =
"+thisl.LASTERROR.ToString(),System.Diagnostics.EventLogEntryType.Error);
}
}
}