C# Directory SetCurrentDirectory() Method

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

Definition

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);
}
CallWhat 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 = pathThe 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

ParameterDescription
pathThe folder to make current. It can be absolute or relative to the old working directory, and it must exist.

Return Value

Return ValueDescription
voidNothing 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);
    }
}

How It Works

  1. Remember where you were. original keeps the old working directory in a variable, which is the key to a tidy exit.
  2. The switch. SetCurrentDirectory(root) changes the process setting. GetCurrentDirectory() now ends with the demo folder name, so printing only the last part is stable.
  3. finally always runs. Even if the code in the try block threw an exception, the old folder would be restored.
  4. 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);
    }
}

How It Works

  1. A short name only. "hello.txt" has no folder part, so .NET adds the working directory in front of it.
  2. The folder was changed first. Because SetCurrentDirectory(root) ran earlier, the new file is created inside root and not in your project folder.
  3. Check with a full path. After restoring, the code builds the full path with Path.Combine(root, ...), and File.Exists says True.
  4. 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.

C#
using System;
using System.IO;

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

        string original = Directory.GetCurrentDirectory();
        try {
            try {
                Directory.SetCurrentDirectory(Path.Combine(root, "missing"));
            } catch (DirectoryNotFoundException) {
                Console.WriteLine("Not found, unchanged: " + (Directory.GetCurrentDirectory() == original));
            }

            Directory.SetCurrentDirectory(child);
            Console.WriteLine(Path.GetFileName(Directory.GetCurrentDirectory()));

            Directory.SetCurrentDirectory("..");
            Console.WriteLine(Path.GetFileName(Directory.GetCurrentDirectory()));
        } finally {
            Directory.SetCurrentDirectory(original);
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. A failed call changes nothing. The folder missing does not exist, so DirectoryNotFoundException is thrown, and the working directory is still original.
  2. Go into the child. SetCurrentDirectory(child) works, and the last part of the current path is child.
  3. Relative to where you are. ".." is resolved from the child folder, so the new working directory is the demo root, codetofun-setcwd-up.
  4. 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.

Back to the method index

See all 15 Directory method guides grouped by what they do.

Method index →

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.

12 people found this page helpful