title: Best .NET way to move many files to and from various directories?
body: <p>I've created a program that moves files to and from various directories. An issue I've come across is when you're trying to move a file and some other program is still using it. And you get an error.  Leaving it there isn't an option, so I can only think of having to keep trying to move it over and over again. This though slows the entire program down, so I create a new thread and let it deal with the problem file and move on to the next. The bigger problem is when you have too many of these problem files and the program now has so many threads trying to move these files, that it just crashes with some kernel.dll error. Here's a sample of the code I use to move the files:</p>
  
  <pre><code>Public Sub MoveIt()
      Try
          File.Move(_FileName, _CopyToFileName)
      Catch ex As Exception
          Threading.Thread.Sleep(5000)
          MoveIt()
      End Try
  End Sub
  </code></pre>
  
  <p>As you can see I try to move the file, and if it errors, I wait and move it again over and over again. I've tried using <code>FileInfo</code> as well, but that crashes WAY sooner than just using the <code>File</code> object.</p>
  
  <p>So has anyone found a fool proof way of moving files without it ever erroring?</p>
  
  <p>Note: It takes a lot of files to make it crash. It'll be fine on the weekend, but by the end of the day on monday, it's done.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I appreciate all the ideas so far. Perhaps I should give more information about what I'm doing. </p>
  
  <p>This is all done in a Windows service. The files MUST be moved. There's no way I can leave any behind. Which is why I must try OVER and OVER again to move these files. The files are used to import data into various databases. Plus there is NO user to tell if the file cannot be moved. Also, this program processes THOUSANDS of files a day.</p>
  
  <p>So with that said. How can I have an efficient program that can move files without any user interaction, and guarantee that all the files get moved?  The programs that create these files eventually give up their hold on them. They get created by FTP, Biztalk and other various services.</p>