HOWTO · Csharp

How to Rename a File in C#

Learn how to rename a file in C# with File.Move or create a renamed copy with File.Copy.

On this page

In C#, a file rename is a move to a new path. Use File.Move when the original should no longer remain at its old path. Use File.Copy when you want a second file under the new name and need to keep the original. Both operations take a source path and a destination path, so changing only the filename means combining the same directory with the new filename.

The examples below create their own temporary files so you can adapt the path setup to your application. Both examples completed successfully in the supplied runtime check and produce no console output.

Rename a file with File.Move

File.Move transfers a file from its source path to its destination path. To rename within one directory, make the destination path use that directory and the new filename. After the move succeeds, the file is at the destination and is no longer available at the source path.

This complete program creates a temporary source file, moves it to a new name in the same directory, and removes the temporary directory when finished:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(directory);
        string sourcePath = Path.Combine(directory, "old-name.txt");
        string destinationPath = Path.Combine(directory, "new-name.txt");

        try
        {
            File.WriteAllText(sourcePath, "Example content");
            File.Move(sourcePath, destinationPath);
        }
        finally
        {
            Directory.Delete(directory, true);
        }
    }
}

The source must exist and the destination path must be usable. If the destination is in another directory, the operation moves the file there as well as changing its name. File.Move is therefore suitable when the old path should stop referring to the file; it does not leave a second copy behind.

Create a renamed copy with File.Copy

Choose File.Copy when both the original and a file with the new name should remain. Its first argument is the existing source file; its second is the path where the copy should be created. The destination can use the same directory for a simple rename-like copy, or a different directory when the copy belongs elsewhere.

This complete program creates a temporary source file, copies it to a new filename, then cleans up both files with their containing directory:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        Directory.CreateDirectory(directory);
        string sourcePath = Path.Combine(directory, "old-name.txt");
        string destinationPath = Path.Combine(directory, "new-name.txt");

        try
        {
            File.WriteAllText(sourcePath, "Example content");
            File.Copy(sourcePath, destinationPath);
        }
        finally
        {
            Directory.Delete(directory, true);
        }
    }
}

After a successful copy, the source remains at its original path and the new file is available at the destination. This is useful when the original must be retained, for example as a backup or as input for another operation. Remember that copying consumes additional storage, and later edits to one file do not update the other automatically.

Handle paths and existing destination files

Use valid C# strings for paths. A verbatim string such as @"C:\\example\\old-name.txt" keeps backslashes from being treated as escape characters; Path.Combine is another practical way to build a path from a directory and filename. The example programs use Path.Combine with a temporary directory so they do not depend on a particular drive or existing folder. In an application, choose a directory the process can access and ensure its parent exists when needed.

The source file must exist before either operation can succeed. A misspelled filename, an unexpected working directory, or a path to a missing folder can prevent the operation. Build paths from known directory values where possible, and check that the source is the file you intend to move or copy before changing it.

By default, File.Move and File.Copy do not overwrite an existing destination file. If a destination name may already be in use, choose a different name or handle that condition before calling the operation. Overwrite overload availability or behavior depends on the target framework, so check the documentation for the framework your application targets before relying on an overload. Do not assume a rename silently replaces an existing file.

In short, use File.Move to relocate the one file to its new path, and use File.Copy to create a separately stored duplicate while retaining the source. The right choice depends on whether the old path should continue to contain the original file.