nokturnal
nokturnal

Reputation: 2879

C#: Monitoring copied or moved files with FileSystemWatcher

I have come across several "solutions" here and across the web but none seem to fit the bill.

What I am looking to do is have an app monitor a folder for new files (either by creation, a move, or a copy) and perform actions on those objects. That being the scenario, I turned to the FileSystemWatcher class to perform this action.

The problem is that the file FileSystemWatcher.Created event is fired before the entire file is created (most noticeably seen through a copy of a large file).

Is there any way to have this event fire at the conclusion of the file creation as opposed to the beginning? I have tried various combination's of the FileSystemWatcher.NofityFilter property with no success.

Thanks in advance! :)

Upvotes: 8

Views: 5720

Answers (5)

Suhaib Ahmad
Suhaib Ahmad

Reputation: 526

Possibly another way to check if the file has been created completely might be to try and open the file for reading. If it can open and read, it means the file has been created successfully. If it fails it means that the file has not been fully created yet.

for(int i = 0; i < timeout; i++)
{
    try
    {
        using ( FileStream  stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)){}
        return true;
    }
    catch (IOException)
    {
        //Thread.Sleep(500);
    }
}
return false;

with the bit above, if the file can be opened and read without exception, it means the file has been created completely and will return true. If an exception is thrown, it means that the file has not been created completely, and depending on your requirements, you can can set timeouts/sleep to keep trying to open file until exhaustion and returns false if it still fails.

Upvotes: 0

You would need to track closing of the file after it's creation and I doubt it's possible with FileSystemWatcher. If you don't find a solution with FileSystemWatcher, take a look at our CallbackFilter product, which lets you track all operations in real-time.

Upvotes: 0

Boris Modylevsky
Boris Modylevsky

Reputation: 3099

I know, that what I am going to tell you does not look elegant. I had also to monitor files that arrive from different places, some of them were large and some small. We found out, that FileSystemWatcher is not reliable for this purpose. If you want to be 100% sure, you can check once in a while, using Timer class and its Elapsed event.

Upvotes: 1

Paul Farry
Paul Farry

Reputation: 4768

I have used a couple of solutions for this situation.

  1. If you can work with the creator of the file and use a renaming scheme for the file. EG. Create the File as __Name_ while being created and at the end of the process rename it to Name and the event will fire and you have a complete file.

  2. When your trigger fires check if you can get an exclusive readonly lock on the file. If you can then the write operation has been completed to the file. (I wrote something about this in another question Keep settings in sync between forms application and windows service (or any n-tier, really))

You could possibly integrate something like #2 into your Changed Event and then you'll get the result.

Upvotes: 6

Ashley Grenon
Ashley Grenon

Reputation: 9565

Hmm interesting problem. I never used the object while watching for big files. Did a little searching and seems one solution is to monitor the Changed event as well. Because once the file is done copying (after created is fired) a changed event is thrown as well (cause the file increased in size)

More details from what I read here: http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/f84bb7c8-b7d5-44da-b0f3-6d1a70415d11/

Upvotes: 1

Related Questions