1

My application is listening to directory and each new file who created need to handle with, so I did a test and listen to my folder and see that if I move big file into this folder the event fired before the whole file is created and this can cause my problem. can I wait until the all file is created ?

public void startListener(string directoryPath)
{
    FileSystemWatcher watcher = new FileSystemWatcher(directoryPath);
    watcher.Filter = "*.avi";
    watcher.Created += watcher_Created;
    watcher.EnableRaisingEvents = true;
}

void watcher_Created(object sender, FileSystemEventArgs e)
{

}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user1269592
  • 691
  • 3
  • 12
  • 24

2 Answers2

0

According the the MSDN docs, multiple OnCreate() and OnChanged() events may be generated when a file is copied from one directory to another.

We need to distinguish two cases here:

  • You are copying the file yourself, so you have control over how the copying is done.

    In this case, it's most efficient to use a temporary filename in the desired folder which does not have the .avi extension (you could for instance use filename.avi.tmp instead of filename.avi), then rename it to the correct name with the .avi extension when you're done copying.

    Then, you subscribe to the Renamed event and watch for files that are renamed to .avi.

  • If you don't have any control over the copying, then you could use one of the techniques described in another answer to this question

Community
  • 1
  • 1
Martin Baulig
  • 3,010
  • 1
  • 17
  • 22
  • 1
    Ok, then use a temporary name that doesn't end in `.avi` (so it won't match your file name filter) in the same directory and rename it into `.avi` when you're done. Renaming a file in the same directory is a single I/O operation, so you'll only get one notification. – Martin Baulig Dec 25 '12 at 15:04
0

What I think you could do, is handle the OnCreated event, by adding the "newly created" files to a local List.

Then on the OnChanged, which will trigger when pieces are updated, check if the file is in that list of newly created ones, and attempt to open the file exclusively, doing something like:

File.Open("someFile.avi", FileMode.Open, FileAccess.Read, FileShare.None)

If you get an IOException, that file is still in use.

Another thing you could do is described in this comment, using the Rename event: https://stackoverflow.com/a/5894697/1373170

In fact that whole SO question might have useful information for you: C# FileSystemWatcher, How to know file copied completely into the watch folder

Community
  • 1
  • 1
Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58
  • Yeah, this could work if another app is copying the file. However, if you're copying the file (so you could change the way how you're copying it), it's much more efficient to use the temp file approach that I described in my answer. – Martin Baulig Dec 25 '12 at 15:54
  • Yes, I was shooting for a solution that would work either way. I'm not certain that there will be much difference regarding efficiency. The cost of attempting to open a file (or to attempt to rename it and catch the exception) is not really that big I think. – Pablo Romeo Dec 25 '12 at 15:58
  • Sure, but you'd have to periodically try to open it in a loop and if you do that 10+ times a second, it's probably less efficient than using FileSystemWatcher. – Martin Baulig Dec 25 '12 at 16:03
  • Right, but hasn't said the response time must be real-time, so you could probably just check that every 10 seconds (check the "watch list") and he'd still be meeting the desired functionality. – Pablo Romeo Dec 25 '12 at 16:09