views:

668

answers:

6

I'm building a C# application that will monitor a specified directory for changes and additions and storing the information in a database.

I would like to avoid checking each individual file for modifications, but I'm not sure if I can completely trust the file access time.

What would be the best method to use for getting recently modified files in a directory?

It would check for modifications only when the user asks it to, it will not be a constantly running service.

+1  A: 

Hmm... interesting question. Initially I'd point you at the FileSystemWatcher class. If you are going to have it work, however, on user request, then it would seem you might need to store off the directory info initially and then compare each time the user requests. I'd probably go with a FileSystemWatcher and just store off your results anyhow.

itsmatt
+1  A: 

I think what you want is provided by the FileSystemWatcher class.

This tutorial describes how to use it to monitor a directory for changes in a simple windows service; How to implement a simple filewatcher Windows service in C#

spoon16
A: 

FileSystemWatcher will not tell you what changed in the files, it will only notify that a change occurred.

In case of addition or deletion of lines in you files, you'll have to check the difference in line count, which means you need to preserve the line count for each file whenever you check them.

Abbas
+5  A: 

Use the FileSystemWatcher object. Here is some code to do what you are looking for.

    // Declares the FileSystemWatcher object
    FileSystemWatcher watcher = new FileSystemWatcher(); 

    // We have to specify the path which has to monitor

     watcher.Path = @"\\somefilepath";     

    // This property specifies which are the events to be monitored
     watcher.NotifyFilter = NotifyFilters.LastAccess |
       NotifyFilters.LastWrite | NotifyFilters.FileName | notifyFilters.DirectoryName; 
    watcher.Filter = "*.*"; // Only watch text files.

    // Add event handlers for specific change events...

    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.Deleted += new FileSystemEventHandler(OnChanged);
    watcher.Renamed += new RenamedEventHandler(OnRenamed);
    // Begin watching.
    watcher.EnableRaisingEvents = true;


    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
    // Specify what is done when a file is changed, created, or deleted.
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
    // Specify what is done when a file is renamed.
    }
MikeJ
+1  A: 

If you only need it to check when the user asks rather then all the time, don't use the FileSystemWatcher. Especially if it's a shared resource - the last thing you want is 50 client machines watching the same shared directory.

It's probably just a typo, but you shouldn't be looking at the file access time, you want to look at the file modification time to pick up changes. Even that's not reliable.

What I would do is implement some sort of checksum function on the file date and byte size, or other file system properties. That way I wouldn't be looking for changes in the complete file - only the properties of it and I can do it on request, rather than trying to hold a connection to a remote resource to monitor it.

A heavier solution would be to do it the other way around, and install a service on the machine hosting the shared drive which could monitor the files and make note of the changes. Then you could query the service rather than touching the files at all - but it's probably overkill.

JBE
A: 

http://www.sentry-go.com/monitor-files-directories-features.aspx?gclid=CKKyr9DCnJkCFSPxDAodJG9CDQ

$79 You can't do this yourself for the cost..