Watching for changes in a file.

  • Thread starter Thread starter Andrew Cooper
  • Start date Start date
A

Andrew Cooper

I've got an application that I'm modifying and the client needs it to
watch a particular file and then do some stuff when that file gets
modified. For example, let's say the file is C:\Test.dat and that it
looks like this...

AcctNum, 500
SomeValue, 4500

Periodically there is another program that changes the Test.dat file
values. The application I'm modifying needs to detect that the Test.dat
file has been modified, read it and then do some stuff with the values.
This isn't supder difficult. I can just place a timer in the
application that checks the File.GetLastWriteTime method for Test.dat
and then acts appropriately. However, that seems kind of inefficient.
Is there an event or something that I can latch onto to accomplish the
same thing without having to constantly fire a timer off?

Thanks for any help.

Andrew Cooper
 
I've got an application that I'm modifying and the client needs it to
watch a particular file and then do some stuff when that file gets
modified.  For example, let's say the file is C:\Test.dat and that it
looks like this...

AcctNum, 500
SomeValue, 4500

Periodically there is another program that changes the Test.dat file
values.  The application I'm modifying needs to detect that the Test.dat
file has been modified, read it and then do some stuff with the values.
  This isn't supder difficult.  I can just place a timer in the
application that checks the File.GetLastWriteTime method for Test.dat
and then acts appropriately.  However, that seems kind of inefficient.
Is there an event or something that I can latch onto to accomplish the
same thing without having to constantly fire a timer off?

Thanks for any help.

Andrew Cooper

Hi,
As i understand from your needs, you'd better think of FileSystemWatch
component in your toolbox, get more info here:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

Thanks,

Onur Güzel
 
Thanks folks!

I knew there had to be a way to do that without a timer.
FileSystemWatcher is exactly what I need.

Also, Patrice, unfortunately I can't change how these programs
communicate. The other program is proprietary and modifying this file
is the most it will do to help me out. I'd much rather use a socket
connection or something to communicate but... alas!

Andrew
 
FileSystemWatcher is very convenient but far from perfect. Some events will
lost espectially with lot of actions simultaneously. You will need you timer
to keep clean job.
 
The system isn't really that busy. The file is only being change every
great once in a while so I doubt I'll run into problems with it changing
too fast and missing something. But I will keep it in mind if things
start behaving strangely.

Andrew
 
Back
Top