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);
Call
What 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
Parameter
Description
sourceDirName
The folder to move. It must exist.
destDirName
The new path of the folder. It must not exist, but its parent must.
Return Value
Return Value
Description
void
Nothing 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);
}
}
Output
False
True
True
How It Works
Same parent, new last part. Both paths sit under root; only draft becomes final. That is how a rename works.
Old name is gone. After Move the first Exists prints False, because nothing lives at draft any more.
New name exists. The second check prints True for final.
Contents travel along.paper.txt was not touched by your code, yet it is found under the new name.
Parent must exist. The code creates archive before the move. Move never creates missing parents the way CreateDirectory does.
A move and a rename together. The folder goes into archive and also becomes inbox-2026 in one step.
Source is gone, contents counted.inbox no longer exists, and GetFiles(target).Length shows both mail files arrived.
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);
}
}
Output
Destination already exists
Source not found
True True
How It Works
No overwrite. Folder b already exists, so Move(a, b) throws IOException and does not merge the two folders.
Different exception for a missing source.ghost was never created, so you get DirectoryNotFoundException, a more specific error than IOException.
Nothing changed. Both failed moves leave the disk as it was, so the last line prints True True.
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.