Skip to content

How to determine if a file copy is complete

Mike

Asked Nov 16, 2005, 9:51 AM EST1 answer
Share
LinkedInX

How can I determine if a file copy has been completed? Upon receiving an FTP'd file, I need to process the file. I'm using the FileSystemWatcher to monitor for the file. I get the create and several change events, however even on the final change event the file isn't always released yet.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I was able to do it using the FileSystemWatcher, hopefully this will help someone else.
    TypeScript
    FileSystemWatcher monitor = new FileSystemWatcher("c:\\test");
    private void button1_Click(object sender, System.EventArgs e)
    {
        monitor.IncludeSubdirectories = false;
         monitor.NotifyFilter = (NotifyFilters.FileName | NotifyFilters.LastAccess);
         monitor.Created += new FileSystemEventHandler(FileCreateHandler);
         monitor.Changed += new FileSystemEventHandler(FileChangeHandler);
         monitor.EnableRaisingEvents = true;
    }
    private void FileChangeHandler(Object obj , System.IO.FileSystemEventArgs fsea)
    {
         try
         {
              FileInfo fi = new FileInfo(fsea.FullPath);
              File.Copy(fsea.FullPath,"c:\\test2\\" + fsea.Name,true);
              textBox1.Text += "The file has been moved." + Environment.NewLine;
              fi.Delete();
              textBox1.Text += "The file has been deleted." + Environment.NewLine;
    
         }
         catch(System.IO.IOException e)
         {
              textBox1.Text += e.Message;
         }
         catch(Exception e)
         {
              textBox1.Text += e.Message;
         }
    }
    private void FileCreateHandler(Object obj , System.IO.FileSystemEventArgs fsea)
    {
         FileInfo fi = new FileInfo(fsea.FullPath);
         textBox1.Text = fsea.Name + " created." + Environment.NewLine;
    }
    0