C# File Move() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, File.Move() moves a file to a new path. When the new path is in the same folder, the move is simply a rename, because C# has no separate rename method for files.

After the call the file is at the new path and the old path no longer exists. By default the destination must not exist. The overload with overwrite is available in .NET Core 3.0 and later and in modern .NET.

This is a static method of System.IO.File. Call it as File.Move(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Pass the current path first and the new path second. Add true to replace an existing destination.

C#
string src = Path.Combine(Path.GetTempPath(), "codetofun-old.txt");
string dst = Path.Combine(Path.GetTempPath(), "codetofun-new.txt");

File.WriteAllText(src, "data");
File.Move(src, dst, true);
CallWhat it does
File.Move(source, dest)Moves or renames the file. Throws IOException if dest exists.
File.Move(source, dest, true)Moves the file and replaces dest if it exists (.NET Core 3.0 and later).
File.Move(a, Path.Combine(folder, name))Moves the file into folder. The folder must already exist.

File.Move works with a path, and FileInfo.MoveTo does the same for an object. Moving inside one drive is quick; moving between drives is a copy followed by a delete.

Library declarations
C#
public static void Move(string sourceFileName, string destFileName);
public static void Move(string sourceFileName, string destFileName, bool overwrite);

Parameters

ParameterDescription
sourceFileNameThe file to move. It must exist.
destFileNameThe new path, including the new file name. Its folder must exist.
overwriteOptional. true replaces an existing destination file. false (the default) throws if it exists.

Return Value

Return ValueDescription
voidNothing is returned. When the call succeeds, the file lives at destFileName.

C# Examples

Example 1 — Rename a File by Moving It

Give a file a new name in the same folder and check both paths.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-move");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string oldPath = Path.Combine(root, "draft.txt");
        string newPath = Path.Combine(root, "final.txt");
        File.WriteAllText(oldPath, "report");

        File.Move(oldPath, newPath);

        Console.WriteLine(File.Exists(oldPath));
        Console.WriteLine(File.Exists(newPath));
        Console.WriteLine(File.ReadAllText(newPath));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Rename is a move. Both paths are in the same folder, so Move only changes the file name from draft.txt to final.txt.
  2. The old name is gone. Exists(oldPath) prints False, while Exists(newPath) prints True. This is the key difference from Copy.
  3. The content travels with the file. Reading final.txt still gives report.

Example 2 — Move a File into Another Folder

Create a target folder first, then move a file into it.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-move-dir");
        if (Directory.Exists(root)) Directory.Delete(root, true);

        string archive = Path.Combine(root, "archive");
        Directory.CreateDirectory(archive);

        string source = Path.Combine(root, "a.txt");
        string target = Path.Combine(archive, "a.txt");
        File.WriteAllText(source, "data");

        File.Move(source, target);

        Console.WriteLine(Path.GetFileName(Path.GetDirectoryName(target)));
        Console.WriteLine(File.Exists(source));
        Console.WriteLine(File.Exists(target));

        Directory.Delete(root, true);
    }
}

How It Works

  1. The folder comes first. CreateDirectory builds archive before the move, because Move never creates folders.
  2. Same name, new place. The target path keeps the file name a.txt but sits inside archive.
  3. Which folder now holds it. GetDirectoryName removes the file name and GetFileName then reads the last folder, which prints archive.
  4. Only one copy remains. The old path is False and the new path is True.

Example 3 — When the Destination Already Exists

See the default failure, then use the overwrite option.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-move-over");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string source = Path.Combine(root, "source.txt");
        string target = Path.Combine(root, "target.txt");
        File.WriteAllText(source, "from-source");
        File.WriteAllText(target, "from-target");

        try {
            File.Move(source, target);
        } catch (IOException) {
            Console.WriteLine("Move blocked");
        }

        File.Move(source, target, true);
        Console.WriteLine(File.ReadAllText(target));
        Console.WriteLine(File.Exists(source));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Both names are taken. target.txt already exists, so the plain two-argument Move throws IOException, and the catch block prints Move blocked.
  2. Nothing changed. The failed call left both files as they were, so the second call still has a source to move.
  3. Overwrite on purpose. Move(source, target, true) replaces target.txt, and reading it gives from-source.
  4. The source is gone. A move leaves no file behind, so the last line prints False.

Frequently Asked Questions

Call File.Move(oldPath, newPath) with both paths in the same folder. C# has no separate File.Rename method, so a move inside one folder is a rename.
Not by default: it throws IOException. Use File.Move(source, dest, true) in .NET Core 3.0 and later, or delete the destination first in older frameworks.
Move transfers the file and removes the old path. Copy leaves the original and makes a second file.
Yes. The system copies the data and then deletes the original, so it takes longer than a move inside one drive. If the copy fails, the original is kept.
No. Use Directory.Move for folders. File.Move expects a file path and throws if the source is a folder.

Next: Open()

Open a file as a FileStream with your choice of mode, access, and sharing.

Open() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful