In C#, Directory.SetCurrentDirectory() changes the working directory: the folder that relative paths use as their start. After the call, File.WriteAllText("hello.txt", ...) writes into the new folder.
The change applies to the whole process, including every thread and every library, so use it with care and put the old folder back when you finish.
This is a static method of System.IO.Directory. Call it as Directory.SetCurrentDirectory(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.
Syntax
Save the old folder first, change to the new one, and restore it in a finally block.
C#
string original = Directory.GetCurrentDirectory();
try {
Directory.SetCurrentDirectory(Path.GetTempPath());
// ... work with relative paths here ...
} finally {
Directory.SetCurrentDirectory(original);
}
Call
What it does
Directory.SetCurrentDirectory(path)
Makes path the working directory. It must exist.
Directory.SetCurrentDirectory("..")
Moves one level up, because a relative path is resolved from the old working directory.
Environment.CurrentDirectory = path
The property form. It does the same work.
Prefer absolute paths built with Path.Combine in real programs. They do not depend on a shared, changeable setting. SetCurrentDirectory is handy for small tools and scripts.
Library declarations
C#
public static void SetCurrentDirectory(string path);
Parameters
Parameter
Description
path
The folder to make current. It can be absolute or relative to the old working directory, and it must exist.
Return Value
Return Value
Description
void
Nothing is returned. Use GetCurrentDirectory() to read the new value.
C# Examples
Example 1 — Change the Working Directory and Restore It
Move into a demo folder, read its name, and go back to the old folder.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-setcwd-demo");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string original = Directory.GetCurrentDirectory();
try {
Directory.SetCurrentDirectory(root);
Console.WriteLine(Path.GetFileName(Directory.GetCurrentDirectory()));
} finally {
Directory.SetCurrentDirectory(original);
}
Console.WriteLine(Directory.GetCurrentDirectory() == original);
Directory.Delete(root);
}
}
Output
codetofun-setcwd-demo
True
How It Works
Remember where you were.original keeps the old working directory in a variable, which is the key to a tidy exit.
The switch.SetCurrentDirectory(root) changes the process setting. GetCurrentDirectory() now ends with the demo folder name, so printing only the last part is stable.
finally always runs. Even if the code in the try block threw an exception, the old folder would be restored.
Safe to delete. The working directory is back at original, so the demo folder is not in use and Delete succeeds on Windows too.
Example 2 — Write a File With a Relative Path
After switching, a short file name is created inside the demo folder.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-setcwd-relative");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string original = Directory.GetCurrentDirectory();
try {
Directory.SetCurrentDirectory(root);
File.WriteAllText("hello.txt", "hi");
} finally {
Directory.SetCurrentDirectory(original);
}
Console.WriteLine(File.Exists(Path.Combine(root, "hello.txt")));
Console.WriteLine(Directory.GetFiles(root).Length);
Directory.Delete(root, true);
}
}
Output
True
1
How It Works
A short name only."hello.txt" has no folder part, so .NET adds the working directory in front of it.
The folder was changed first. Because SetCurrentDirectory(root) ran earlier, the new file is created inside root and not in your project folder.
Check with a full path. After restoring, the code builds the full path with Path.Combine(root, ...), and File.Exists says True.
Exactly one file.GetFiles(root).Length is 1, which shows nothing else was written there.
Example 3 — Handle a Missing Folder and Walk Up With ..
A missing folder throws and changes nothing. A relative .. climbs one level.
Not found, unchanged: True
child
codetofun-setcwd-up
How It Works
A failed call changes nothing. The folder missing does not exist, so DirectoryNotFoundException is thrown, and the working directory is still original.
Go into the child.SetCurrentDirectory(child) works, and the last part of the current path is child.
Relative to where you are.".." is resolved from the child folder, so the new working directory is the demo root, codetofun-setcwd-up.
Restore, then delete. The outer finally returns to the old folder, which makes the recursive Delete at the end safe.
Frequently Asked Questions
It changes the working directory of the whole process. Relative paths, such as "data.txt", then start from the new folder.
It throws DirectoryNotFoundException and the working directory stays the same.
Yes. The working directory belongs to the process, so every thread sees the change. This is one reason to prefer absolute paths.
On Windows the process holds the current directory open. Set the working directory to another folder first, then delete.
Usually no. Build full paths with Path.Combine instead. SetCurrentDirectory is more useful in small tools and test scripts.