Over a million developers have joined DZone.

'Move with Replace' C# method

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

You 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.
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.Move(sourceFileName, destFileName);


Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}