Christian Sparre
Christian Sparre

Reputation: 965

Periodically finding changed files in a directory using .NET

I'm trying to find the most reliable way of finding new and modified files in a directory using C# and .NET. I'm not looking for a real time solution, I want to check for changes at given times. It could be every 5 minutes or every hour etc.

We have CreationTime and LastWriteTime on the FileInfo object, and this seems to be enough to get new and modified files. But if a file is renamed none of the available dates are changed and the file will be missed if we just look at CreationTime and LastWriteTime.

At the moment i'm maintaining af "snapshot" of the files in the directory including the time of the last check for changes. This enables me to compare all the files in the directory with files in the snapshot, if the snapshot is missing a file it is either new or renamed.

Is this the only way? Rr am I missing something. I'm not going to use FileSystemWatcher as it seems pretty "buggy" and is required to run all the time.

Any suggestions are very welcome.

Merry Christmas!

Upvotes: 2

Views: 3176

Answers (5)

SANDIP
SANDIP

Reputation: 89

File system watcher class in .net provides two methods:-

  1. OnChanged
  2. OnRenamed

You can set the EnableRaisingEvents to true and that is!!!! Every thing is simple with Dot Net chill!!!

Upvotes: 0

Louis Kottmann
Louis Kottmann

Reputation: 16638

Your problem looks very much like a Database with no primary key.

If you asssign, say, a GUID to each file in that folder and check for that GUID instead of the filename, your application will be much more reliable.

So that's the theory, in practice, we're talking metadata. Depending on your system, and the files contained in that folder, you could use Alternate Data Streams.

Here is a SO question about it.

It boils down to having information on a file that is not stored within the file, it is merely linked to it.
You can then look it up in a DOS box:

notepade.exe myfile.txt:MYGUID

It requires the system to use NTFS.

HTH.

Upvotes: 1

ken2k
ken2k

Reputation: 48995

Use the FileSystemWatcher class, it's the good way. Maybe you could be more specific with the

as it seems pretty "buggy"

EDIT: FileSystemWatcher does support renaming events.

Upvotes: 6

Ironicnet
Ironicnet

Reputation: 567

A very primitive approach would use a command of "dir" and comparing outputs...

Here is some info on params: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx?mfr=true

Along with you snapshot of dates, you can compare the outputs of dir... its very fast and low on resources consuming...

Upvotes: 0

ChrisF
ChrisF

Reputation: 137158

The Microsoft Sync Framework has components for synchronising files.

The framework covers all data types and data storage and the file system component should be more reliable than the FileSystemWatcher. As it says in the MSDN:

It can be used to synchronize files and folders in NTFS, FAT, or SMB file systems. The directories to synchronize can be local or remote; they do not have to be of the same file system. An application can use static filters to exclude or include files either by listing them explicitly or by using wildcard characters (such as *.txt). Or the application can set filters that exclude whole subfolders. An application can also register to receive notification of file synchronization progress

I know you really only want to know when files have changed, but given that you've already dismissed the FileSystemWatcher route it might be the only reliable route (other than doing what you are in maintaining a snapshot yourself).

Upvotes: 1

Related Questions