Ayush
Ayush

Reputation: 42450

Program needs to read file everytime it changes

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

Upvotes: 1

Views: 224

Answers (3)

Iain Ward
Iain Ward

Reputation: 9936

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

Upvotes: 0

Robaticus
Robaticus

Reputation: 23157

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

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500525

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.

Upvotes: 6

Related Questions