'Move with Replace' C# method
Join the DZone community and get the full member experience.
Join For FreeYou may not realize that System.IO.File.Move() doesn't support overwriting of an existing file. In fact, it will throw an IOException if a file with the same path as sourceDestFilename already exists.
As a workaround to that limitation, I wrote a simple, yet useful wrapper method below that allows for overwriting the destination file.
As a workaround to that limitation, I wrote a simple, yet useful wrapper method below that allows for overwriting the destination file.
public static void MoveWithReplace(string sourceFileName, string destFileName) { //first, delete target file if exists, as File.Move() does not support overwrite if (File.Exists(destFileName)) { File.Delete(destFileName); } File.Move(sourceFileName, destFileName); }
Overwriting (computer science)
Workaround
Opinions expressed by DZone contributors are their own.
Comments