Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
hi
Description:
I used FileSystemWatcher to wach a folder (D:\Watch) in my local system. If a zip folder comes that folder (D:\Watch), the filesystemwatcher raise created event. Finally the zip folder is unzipped.

Problem:
if copy a 80MB of Zip file into the folder (D:\Watch). It takes some 1-2 min time to creating zip file in that location (D:\Watch). But the filesystemwatcher raise the created event before complete the copy. so that my extact function is failed.

Please help me...

sample code:

C#
namespace myproject
{
    public sealed class extractor
    {

      private FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
      
      public extractor()
      {   fileSystemWatcher.Path = @"D:\Watch";
           fileSystemWatcher.Filter = "*.zip":;
           fileSystemWatcher.Created += new FileSystemEventHandler(FileSystemWatcherCreated);
           fileSystemWatcher.EnableRaisingEvents = true;
      }

     private void FileSystemWatcherCreated(object sender, FileSystemEventArgs e)
     {
       Extract(e.fullPath);
     }    


    }
}
Posted
Comments
RDBurmon 8-Jun-12 8:19am    
Thanks Everyone who replied to this thread , So Priya , I think you have got enough response and you should be able to mark it as your answer and close the thread.

The documentation [^]for the FileSystemWatcher class specifically states your observed behaviour

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

You could check that a lock to the file has been released, and then know that the copying is complete?
 
Share this answer
 
an excellent example is this.

http://stackoverflow.com/questions/3822175/filesystem-watcher-and-large-files[^]

If not you could create a queue. Each time the watcher raise a created event you could put that into a queue. You could then create a timer to "Poll" the queue and process a file later. This could be used in conjunction with the "IsLocked" file example.
 
Share this answer
 
check this tool:
Smart Watcher[^]
 
Share this answer
 
This is rather brute force approach, but I use it for years. :)

C#
private void FileSystemWatcherCreated(object sender,FileSystemEventArgs e)
{
    //check if it's available for operation      
    while(true)
    {
        try
        {        
            File.Open(e.fullPath, FileMode.Open);
            //we can go to next statement
            break;
        }
        catch(IOException)
        {
           //operation hasn't finished, let's wait a moment
           Thread.Sleep(500);
        }
    }
    //Do your job
    Extract(e.fullPath);
}
 
Share this answer
 
Comments
PriyaRajavelu 18-Jun-12 23:41pm    
thanks for ur reply but if the zip file size is 5 - 10 GB then is it work ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900