C# Directory Move() Method

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

Definition

In C#, Directory.Move() moves a folder and everything inside it to a new place. Because a move to a new name in the same parent is a rename, this is also the method you use to rename a folder.

On the same drive the move is just a change of name in the file system, so it is fast even for a big folder.

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

Syntax

Give the current path first and the new path second. The new path must not exist yet.

C#
string from = Path.Combine(Path.GetTempPath(), "codetofun-move-old");
string to = Path.Combine(Path.GetTempPath(), "codetofun-move-new");

Directory.CreateDirectory(from);
Directory.Move(from, to);
CallWhat it does
Directory.Move(old, new)Moves the folder to new. When both are in the same parent, it renames the folder.
Directory.Move("a/x", "b/x")Moves x into folder b. The folder b must already exist.
Directory.Exists(new)Check this first if you are unsure. A destination that exists makes Move throw.

There is no overwrite option. If you want to replace a folder, delete the old destination first. To move a single file, use File.Move instead.

Library declarations
C#
public static void Move(string sourceDirName, string destDirName);

Parameters

ParameterDescription
sourceDirNameThe folder to move. It must exist.
destDirNameThe new path of the folder. It must not exist, but its parent must.

Return Value

Return ValueDescription
voidNothing is returned. When the call finishes, the folder is at the new path.

C# Examples

Example 1 — Rename a Folder

Give a folder a new name inside the same parent and check that the file inside came along.

C#
using System;
using System.IO;

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

        string oldPath = Path.Combine(root, "draft");
        string newPath = Path.Combine(root, "final");
        Directory.CreateDirectory(oldPath);
        File.WriteAllText(Path.Combine(oldPath, "paper.txt"), "text");

        Directory.Move(oldPath, newPath);

        Console.WriteLine(Directory.Exists(oldPath));
        Console.WriteLine(Directory.Exists(newPath));
        Console.WriteLine(File.Exists(Path.Combine(newPath, "paper.txt")));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Same parent, new last part. Both paths sit under root; only draft becomes final. That is how a rename works.
  2. Old name is gone. After Move the first Exists prints False, because nothing lives at draft any more.
  3. New name exists. The second check prints True for final.
  4. Contents travel along. paper.txt was not touched by your code, yet it is found under the new name.

Example 2 — Move a Folder Into Another Folder

Create an archive folder and move inbox under it.

C#
using System;
using System.IO;

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

        string inbox = Path.Combine(root, "inbox");
        string archive = Path.Combine(root, "archive");
        Directory.CreateDirectory(inbox);
        Directory.CreateDirectory(archive);
        File.WriteAllText(Path.Combine(inbox, "mail1.txt"), "hi");
        File.WriteAllText(Path.Combine(inbox, "mail2.txt"), "hello");

        string target = Path.Combine(archive, "inbox-2026");
        Directory.Move(inbox, target);

        Console.WriteLine(Directory.Exists(inbox));
        Console.WriteLine(Directory.GetFiles(target).Length);
        Console.WriteLine(Path.GetFileName(Path.GetDirectoryName(target)));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Parent must exist. The code creates archive before the move. Move never creates missing parents the way CreateDirectory does.
  2. A move and a rename together. The folder goes into archive and also becomes inbox-2026 in one step.
  3. Source is gone, contents counted. inbox no longer exists, and GetFiles(target).Length shows both mail files arrived.
  4. Find where it went. GetDirectoryName gives the parent path, and GetFileName turns that into the leaf name archive.

Example 3 — Handle the Two Common Move Errors

Try to move onto a folder that exists, then try to move a folder that does not.

C#
using System;
using System.IO;

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

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

        try {
            Directory.Move(a, b);
        } catch (IOException) {
            Console.WriteLine("Destination already exists");
        }

        try {
            Directory.Move(Path.Combine(root, "ghost"), Path.Combine(root, "c"));
        } catch (DirectoryNotFoundException) {
            Console.WriteLine("Source not found");
        }

        Console.WriteLine(Directory.Exists(a) + " " + Directory.Exists(b));
        Directory.Delete(root, true);
    }
}

How It Works

  1. No overwrite. Folder b already exists, so Move(a, b) throws IOException and does not merge the two folders.
  2. Different exception for a missing source. ghost was never created, so you get DirectoryNotFoundException, a more specific error than IOException.
  3. Nothing changed. Both failed moves leave the disk as it was, so the last line prints True True.
  4. Catch the specific one. DirectoryNotFoundException is a kind of IOException, so catch the specific class first if you handle both.

Frequently Asked Questions

Use Directory.Move(oldPath, newPath) where both paths have the same parent folder. A move to a new name in the same place is a rename.
No. If the destination exists, Move throws IOException. Delete the old destination first if you really want to replace it.
No. The parent of the destination must already exist. Call Directory.CreateDirectory on the parent first.
Older .NET versions throw IOException for a different drive. Newer versions may work on some systems, but the safe way is to copy the files and then delete the original folder.
Use File.Move(source, destination). Directory.Move is only for folders.

Next: SetCurrentDirectory()

Change the working directory for the whole program; relative paths follow it.

SetCurrentDirectory() 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.

11 people found this page helpful