.NET Best Way to move many files to and from various directories??
- by Dan
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:
Public Sub MoveIt()
Try
File.Move(_FileName, _CopyToFileName)
Catch ex As Exception
Threading.Thread.Sleep(5000)
MoveIt()
End Try
End Sub
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 FileInfo as well, but that crashes WAY sooner than just using the File object.
So has anyone found a fool proof way of moving files without it ever erroring?
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.