xander
xander

Reputation: 1447

How to get notified if a file was modified outside of my program?

A portion of my program has to keep track of a group of files, including their paths and hash codes in the memory.

Problem is, a user might like to edit the file using an external program at any point in time.

If this happens, I want my program to get notified so it updates the corresponding hash code of that file in its memory in addition to a number of other possible actions.

Ideally, I would like a new thread or a dedicated background thread to be notified and do the necessary action instead of the main GUI thread. I am using ConcurrentDictionary and similar thread-safe data structures.

I am using C# 5, Windows 7.

Upvotes: 1

Views: 589

Answers (3)

Alexander
Alexander

Reputation: 2477

The FileSystemWatcher component can do that for you:
http://msdn.microsoft.com/en-us/library/342da5th(v=vs.71).aspx

I've tried it before in a similar fashion like you describe, works perfectly.

Upvotes: 2

Jonas W
Jonas W

Reputation: 3250

Have you checked the FileSystemWatcher class?

Perhaps this class can help you to get notifies when files are changing.

Upvotes: 2

Habib
Habib

Reputation: 223187

You are looking for FileSystemWatcher

Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.

Upvotes: 3

Related Questions