473,395 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

FileSystemWatcher bug?

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
Nov 16 '05 #1
2 4482
John Lee wrote:
// 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(WatcherChangeType s.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() ?
Nov 16 '05 #2
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" <mi**********@lvrREMOVE.deCAPS> wrote in message
news:41beb607$1@news...
John Lee wrote:
// 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(WatcherChangeType s.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() ?

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
1
by: Troy Murphy | last post by:
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...
7
by: Allen Anderson | last post by:
I'm trying to figure out a way to catch when a file has been written to a directory. I currently have it where I can catch when the file begins writing, but this isn't helpful as I need to know...
2
by: Jet Leung | last post by:
Hi all, I had made a program to watching files in my directory. I had used a instance of FileSystemWatcher to do my work.And I had add some events of the FileSystemWatcher , for example onChange,...
13
by: David | last post by:
I have been working on trying to write a directory watcher service. One of the requirments is that it be able to watch multiple directories, not sub directories of one parent directory, but just...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
5
by: Goran Djuranovic | last post by:
Hi all, I have a file system watcher service that works fine on a local hard drive, but will not work across the network. I tried both: mapping the drive and "\\..." path both no luck. I don't...
5
by: =?Utf-8?B?Sm9obiBT?= | last post by:
I am trying to find out if there is a way to tell if there is already a filesystemwatcher (created by a webservice) monitoring a folder. I have a webservice that creates a filesystemwatcher,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.