FileSystemWatcher bug?

J

John Lee

Hi,

I wrote a small program that copy a list of files to a specified folder one
by one, i.e. this app will copy one file into the specified folder and wait
until it's consumed (deleted) then copy another one in. Here is what my
code:

// other codes to get a list of files
while (morefiles)
{
// copy one file to destination
string destFileName = "DestFileName";

System.IO.File.Copy(sourceFileName, destFileName);

// wait for the file being removed
watcher.Path = txtTargetFolder.Text ;
watcher.Filter = Path.GetFileName(destFileName); //only interested if
this file gets deleted
watcher.NotifyFilter = NotifyFilters.FileName ;
watcher.WaitForChanged(WatcherChangeTypes.Deleted);
}

It works OK if no other application would touch the destination folder - it
stop working if someone else drop a file into the destination folder and
that file gets deleted, after that this watcher will longer re-act to any
file deletion.

Is this a bug of FileSystemWatcher or there is a flaw in my solution?

Thanks very much!
John
 
M

Michael Voss

John said:
// other codes to get a list of files
while (morefiles)
{
// copy one file to destination
string destFileName = "DestFileName";

System.IO.File.Copy(sourceFileName, destFileName);

// wait for the file being removed
watcher.Path = txtTargetFolder.Text ;
watcher.Filter = Path.GetFileName(destFileName); //only interested if
this file gets deleted
watcher.NotifyFilter = NotifyFilters.FileName ;
watcher.WaitForChanged(WatcherChangeTypes.Deleted);
}

It works OK if no other application would touch the destination folder - it
stop working if someone else drop a file into the destination folder and
that file gets deleted, after that this watcher will longer re-act to any
file deletion.

Is this a bug of FileSystemWatcher or there is a flaw in my solution?
[...snip...]

I'm not sure, but maybe
FileSystemWatcher.WaitForChanged(WatcherChangeTypes.Deleted) ignores your
Filter, so it will react on any file deletion. It will definitely wait for a
single "file deleted" event and continue with the execution afterwards.

Have you tried something like

this.watcher = new FileSystemWatcher(txtTargetFolder.Text,
Path.GetFileName(destFileName));
// Maybe you'd better look for any event, no matter what files are
// beeing deleted. Check in callback-Method instead
this.watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.CreationTime | NotifyFilters.FileName;
this.watcher.Deleted += new FileSystemEventHandler(this.fileDeleted);
this.watcher.EnableRaisingEvents = true;

and creating a callback method

private void fileDeleted(object source, FileSystemEventArgs e)
{
// Check wether your file has been deleted;
// info is in FileSystemArgs. If it is,
// copy next File, if there is one left.
// change watcher.Filter to new filename.
// If another file has been deleted, do nothing.
// If there is no more file to copy, just
// do
// this.watcher.EnableRaisingEvents(false).

}


instead of watcher.WaitForChanged() ?
 
J

John Lee

Thanks Michael!

You are right - I can use the watcher.Deleted event and always remember my
current filename. BUT I think it would be easier to just do it in a loop and
I also want this bug (I think it's a bug) fixed.

Thanks!
John

Michael Voss said:
John said:
// other codes to get a list of files
while (morefiles)
{
// copy one file to destination
string destFileName = "DestFileName";

System.IO.File.Copy(sourceFileName, destFileName);

// wait for the file being removed
watcher.Path = txtTargetFolder.Text ;
watcher.Filter = Path.GetFileName(destFileName); //only interested if
this file gets deleted
watcher.NotifyFilter = NotifyFilters.FileName ;
watcher.WaitForChanged(WatcherChangeTypes.Deleted);
}

It works OK if no other application would touch the destination folder - it
stop working if someone else drop a file into the destination folder and
that file gets deleted, after that this watcher will longer re-act to any
file deletion.

Is this a bug of FileSystemWatcher or there is a flaw in my solution?
[...snip...]

I'm not sure, but maybe
FileSystemWatcher.WaitForChanged(WatcherChangeTypes.Deleted) ignores your
Filter, so it will react on any file deletion. It will definitely wait for
a
single "file deleted" event and continue with the execution afterwards.

Have you tried something like

this.watcher = new FileSystemWatcher(txtTargetFolder.Text,
Path.GetFileName(destFileName));
// Maybe you'd better look for any event, no matter what files are
// beeing deleted. Check in callback-Method instead
this.watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.CreationTime | NotifyFilters.FileName;
this.watcher.Deleted += new FileSystemEventHandler(this.fileDeleted);
this.watcher.EnableRaisingEvents = true;

and creating a callback method

private void fileDeleted(object source, FileSystemEventArgs e)
{
// Check wether your file has been deleted;
// info is in FileSystemArgs. If it is,
// copy next File, if there is one left.
// change watcher.Filter to new filename.
// If another file has been deleted, do nothing.
// If there is no more file to copy, just
// do
// this.watcher.EnableRaisingEvents(false).

}


instead of watcher.WaitForChanged() ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top