Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

'Move with Replace' C# method

DZone's Guide to

'Move with Replace' C# method

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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.Delete(destFileName);
    } 

    File.Move(sourceFileName, destFileName);

}

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.

Topics:
dotnet ,.net ,framework ,c# ,move ,utility ,io

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}