0

I understand that there are so many similar questions on Stackoverflow and other sites. I didn't quite get the answer I am looking for. Hence I make this attempt to ask the question.

I have a requirement to move files from SOURCE folder to DEST folder. And also check if the files have moved successfully.

I created a simple test harness with the following code

  private void btnStartWatch_Click(object sender, EventArgs e)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = source;
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Filter = "*.*";
            watcher.Changed += Watcher_Changed;
            watcher.EnableRaisingEvents = true;

        }

 private void Watcher_Changed(object sender, FileSystemEventArgs e)
        {

            try
            {
                // Debug.WriteLine("File detected: " + e.Name);
                File.Move(Path.Combine(source, e.Name), Path.Combine(target, e.Name));

            }
            catch (Exception ex)
            {

                // do not throw ex for now.
               // I get into this block several times even for a single file??

            }
}

The above code seems to do the trick. However I do not know how many files have been moved. How do I check if the files arrived at the SOURCE have been moved to DEST?

There is no completion event on FileSystemWatcher, so I thought I can use BackgroundWorker instead. What do you guys think?

  1. The background worker gets Triggered every 10 Seconds. Checks

  2. Directory.GetFiles to a get a list of files (list x) at SOURCE

  3. Go through each file and check if they exist at DEST?
  4. If the file do not exist Move it Finally go through the list (list X) and check all files are present in DEST.
Abe
  • 1,879
  • 2
  • 24
  • 39
  • Sounds great, what's your question? For moving files from a pickup directory to a target directory plenty of tools already exist, why do you want to reinvent the wheel? Note that you'll have to cater for files-in-transfer that are still being written (the exception occurring in your code), transfers that get cancelled, virus scanners and other applications locking the file, and so on. – CodeCaster May 16 '17 at 10:46
  • Thank you for your response. I have updated my question and I look forward to hearing from you. – Abe May 16 '17 at 11:29

1 Answers1

0

I'm not 100% sure what your question is, but I have some observations.

First, I've found that FileSystemWatcher can sometimes miss files - this happens when the internal buffer fills up. See the following links for more information and fixes (increase the buffer size), however I switched to polling instead for reliability reasons (speed wasn't as important as having the file being picked up 100% of the time).

Second, your File.Move call is failing because the file is still locked - it will remain locked until the source application has finished writing to the file, exactly how long this will take depends on the source application and the file size. This can happen even if the user copies or moves the file in Windows Explorer. There is no reliable way of telling whether or not the file is still locked other than trying to lock it yourself - this is what File.Move does internally anyway, so you are better off just trying to move the file and handling the failure.

Note that while most applications will lock files in a way that prevents you from moving the file, it is possible to create a file which can be moved even while the file is open. When this happens the destination file can still be written to by the writing application even though its in a different folder.

Once File.Move has succeeded, the file has been moved. You shouldn't need to check that the file exists in the destination folder.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367