From the documentation of File.Move
:
Note that if you attempt to replace a file by moving a file of the same name into that directory, you get an IOException. You cannot use the Move method to overwrite an existing file.
In short, you can't overwrite on Move
, so in order to facilitate overwriting on Move I mimic the behavior by doing a File.Copy
followed by a File.Delete
. Something like:
if (File.Exists(dstFileName))
{
// System.IO.File.Move cannot be used to overwrite existing files, so we're going
// to simulate that behavior with a Copy & Delete.
File.Copy(procContext.FileName, dstFileName);
File.Delete(procContext.FileName);
}
else
File.Move(procContext.FileName, dstFileName);
My question is: Are there any situations that I need to guard against which could lead to the source file being deleted without it first being successfully copied?
My understanding from reading the documentation is that since File.Copy
doesn't return anything that it should throw an exception in any case that it doesn't succeed. Has anyone encountered any situations where this isn't true?