1

I have a program that reads an XML file (for now, on local computer.) and loads the data into a list of struct.

How can I make it such that if I execute it, it does the above but then waits to keep checking for any change to the file. Should the file be changed, it reads the file all over again.

Do I need to create a file watcher service as described here:

http://www.codeproject.com/KB/files/C__FileWatcher.aspx

Ayush
  • 41,754
  • 51
  • 164
  • 239
  • possible duplicate of [Detect File Read in C#](http://stackoverflow.com/questions/3621661/detect-file-read-in-c) – Brian Sep 14 '10 at 17:11

3 Answers3

6

You need FileSystemWatcher - the docs give examples.

Basically you create an instance, give it a filter (which would be your exact file in this case), hook up an event handler (probably the Changed event in your case) and then set EnableRaisingEvents to true.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

You'll want to look at the System.IO.FileSystemWatcher class. You can have it raise an event in your code when the file is changed.

Details can be found on MSDN: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

Robaticus
  • 22,857
  • 5
  • 54
  • 63
0

Look at the FileSystemWatcher class. You can point it at your XML file and when it changes, it will fire an event so you can then read the file again

Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • @Robaticus: Totally! I dont know how he gets in so quickly, think he must have cultivated an army to answer all these questions for him... – Iain Ward Sep 14 '10 at 18:25