Monitoring a Directory

  • Thread starter Thread starter Steven Blair
  • Start date Start date
S

Steven Blair

Hi,

I need to monitor a directory for files. When a file is moved into this
direcotry I need some sort of event to be called and the file will then
be picked up.
I have done this type of thing using MFC, and used FindFirstFile, making
a call to that ever x milliseconds. I am just wondering if .NET has
something different to offer for problems like this ?

Regards,

Steven
 
Steven,

Please have a look at the FileSystemWatcher class in the .NET
Framework. Also do a search about it in this newsgroup and read up
on some of the issues that people have run into, to save yourself some
time =)

HTH,

//Andreas

--
ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
Hi,

I need to monitor a directory for files. When a file is moved into this
direcotry I need some sort of event to be called and the file will then
be picked up.
I have done this type of thing using MFC, and used FindFirstFile, making
a call to that ever x milliseconds. I am just wondering if .NET has
something different to offer for problems like this ?

Regards,

Steven
 
Steven Blair said:
I need to monitor a directory for files. When a file is moved into this
direcotry I need some sort of event to be called and the file will then
be picked up.
I have done this type of thing using MFC, and used FindFirstFile, making
a call to that ever x milliseconds. I am just wondering if .NET has
something different to offer for problems like this ?

Well, there's FileSystemWatcher. However, you should note that it's not
available on all platforms, and I've read various reports about it not
working terribly reliably :(
 
If by "picking up" the file, you mean you will be removing it from the
directory (that is, any file in the directory is in a queue to be processed)
then I think I would use a Timer (either the System.Timers.Timer class or
the System.Threading.Timer class) set it for a few milliseconds (or 1/10 of
a second, whatever works for your app) and then use the
System.IO.Directory.GetFiles function to get an array of Filename in the
directory, or use the System.IO.DirectoryInfo class, and the GetFiles method
to get an enumertor of FileInfo objects, one for every file in the
directory.
 
Ok thanks for the advice.

This is the what I have done:

Created a Windows service which uses a FileSystemWatcher object. So
anytime a file gets move into the directory, the appropiate event is
being. I take the filename(s) and add them to a List. This is being
processed on a thread. I have another thread which is working through
the array (take the first element, process it and remove it from array).
The problem I encountered was when the service started up, the
FileSystemWatcher wouldnt detect files already in the direcotry, so I
had to use a FileInfo on startup, take the filenames in the direcotry
and copy them to the List.

Havent fully tested this yet, but it does look like its working.

Anyone got any thoughts on this method of processing ?

Regards,

Steven
 
Back
Top