Tim F.
Tim F.

Reputation: 298

Why does NET movefile sometimes leave the source file on the hard drive

I have a situation, admittedly easily fixed, where I have a single one line piece of code as follows:-

My.Computer.FileSystem.MoveFile(f_source, f_dest, True)

The true is for the overwrite option.

I have an instance during bulk file moving where sometimes, just sometimes, a source file is left behind but the destination file is copied successfully.

There is error trapping surrounding it (VB.NET/try/catch) and yet no error is triggered. It only seems to happen during remote (VPN) access, the same operation on site hasn't manifested.

I suspect it is occurring during some kind of buffer filling since the move is to and from the remove drive which is a bit of a round Robin.

I have since added a check after the move to see if both files exist and to delete the source when they do. I can confirm that this has been triggered and it has, so far, cured my problem.

I am not using any background or threading operations. I am surprised that control is being handed back to my program with it unfinished and without an error. Is this a known problem?

I haven't tested the other movefile options and, of course, I could always just copy then delete so its no biggie but it did catch me out.

Upvotes: 0

Views: 428

Answers (1)

Chris Fannin
Chris Fannin

Reputation: 1294

I would assume that it is following the same rules at the System.IO.File.Move method. In the remarks for that, it says this:

If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.

There might be some type of latency in the I/O, especially if any type of file scanning (antivirus, etc) is going on.

Upvotes: 2

Related Questions