5

I have a folder with the following structure

Parent/
    Child1/
        GrandChild1/
             File1.txt

I need to query Parent folder and find out if Child1 has changed.

Changed = A new file was add/update/deleted.

The Child1 folder DateModified is not updated. Only the GrandChild1 date modified was updated when changes occurs. I am trying to avoid going to the file level to determine if the rootparent has changed. since there will be many folders and sub folder. I just need to know if Child1 has changed.

I do not want to use FileSystemWatcher, since I am running this as a scheduled job and not watching it LIVE.

Arcadian
  • 4,312
  • 12
  • 64
  • 107

3 Answers3

6

User FileSystemWatcher. Remember to enable raising events since it is a common mistake (watchfolder.EnableRaisingEvents = true;).

The FileSystemWatcher may prove not to be optimal from a performance perspective. If that is an issue for you, you might implement a CRC check with a Timer to check for changes of the files and folders you are interested in.

Essentially, what I would do is to generate a CRC32 hash for the entire folder I am watching (and save it away into variable A) and when I decide it is time to check for changes, you simply calculate a new CRC32 hash for the same folder (into variable B). You then compare A with B and if they don´t match, something has changed. Really not that difficult.

Reference:

Community
  • 1
  • 1
Marcus
  • 8,230
  • 11
  • 61
  • 88
  • Certainly the simplest solution. But FileSystemWatcher can have [some](http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes) reliability issues. – hometoast Oct 11 '12 at 14:08
  • I have used it a couple of times before and never had any problems. Can you elaborate? – Marcus Oct 11 '12 at 14:09
  • I am runnng a schedule job and cannot use the FileSystemWatcher. Since I am not running live. – Arcadian Oct 11 '12 at 14:13
  • 1
    Ok, see my edit. Consider implementing a CRC solution to check for changes! – Marcus Oct 11 '12 at 14:15
  • @Marcus, it's no longer relavant to the OP's question, but this question addresses some of the issues devs have had with it: http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes – hometoast Oct 11 '12 at 16:37
1

Have you tried the file system watcher? You can monitor local drives for changes from a given path, and then if necessary, ignore or process the fact they changed.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
1

You can use the FileSystemWatcher Class for this.

Alternatively, if you would rather schedule a Task to run, Weekly, for example, you might want to have a look at: http://taskscheduler.codeplex.com/ and http://www.emoreau.com/Entries/Articles/2004/08/Interfacing-the-Windows-Task-Scheduler.aspx

And here's a link to the Windows Task Schedular API

jay_t55
  • 11,362
  • 28
  • 103
  • 174
  • Is there a way without having to watch it? I want to run this check once a week and not to have it monitored – Arcadian Oct 11 '12 at 14:24
  • You could programmatically schedule a Windows Task from within your application, that Watches a directory, or sub-directory at a given date and time (or, Weekly). See my answer for further details. – jay_t55 Oct 11 '12 at 14:30