In C#, Directory.GetCurrentDirectory() returns the current working directory: the folder your program treats as "here". Any relative path such as "data.txt" is resolved from this folder.
The value depends on how the program was started, not on where the .exe file sits. A program started from a terminal uses the terminal's folder, and one started from a shortcut can use a different folder.
This is a static method of System.IO.Directory. Call it as Directory.GetCurrentDirectory(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.
Syntax
The method takes no arguments and returns a path string.
C#
string cwd = Directory.GetCurrentDirectory();
Console.WriteLine(Path.IsPathRooted(cwd));
Console.WriteLine(Path.GetFileName(cwd)); // varies by machine
Call
What it does
Directory.GetCurrentDirectory()
Returns the full path of the working directory.
Environment.CurrentDirectory
A property that gives the same value (and can also set it).
AppContext.BaseDirectory
The folder of the program files. It is not always the same as the working directory.
The result differs on every machine, so tutorial code should not print the full path. These examples print booleans or only the last folder name through Path.GetFileName, and the text says when a value varies.
Library declarations
C#
public static string GetCurrentDirectory();
Parameters
Parameter
Description
None
GetCurrentDirectory() takes no parameters.
Return Value
Return Value
Description
string
The absolute path of the current working directory. A root folder ends with a separator (such as C:\); other folders do not.
C# Examples
Example 1 — Check the Working Directory Without Printing It
The path is different on each computer, so print facts about it instead.
C#
using System;
using System.IO;
class Program {
static void Main() {
string cwd = Directory.GetCurrentDirectory();
Console.WriteLine(cwd.Length > 0);
Console.WriteLine(Path.IsPathRooted(cwd));
Console.WriteLine(Directory.Exists(cwd));
}
}
Output
True
True
True
How It Works
Not empty. The method always returns text, so cwd.Length > 0 is True. Printing cwd itself would show a different path for each reader.
An absolute path.Path.IsPathRooted is True because the result starts from a drive or from /, never from a relative piece such as ...
A real folder. The working directory exists, so Directory.Exists is True.
Example 2 — See How a Relative Path Becomes Full
Compare Path.GetFullPath of a short name with the working directory plus that name.
C#
using System;
using System.IO;
class Program {
static void Main() {
string cwd = Directory.GetCurrentDirectory();
string viaFullPath = Path.GetFullPath("notes.txt");
string viaCombine = Path.Combine(cwd, "notes.txt");
Console.WriteLine(viaFullPath == viaCombine);
Console.WriteLine(Path.GetFileName(viaFullPath));
Console.WriteLine(Path.IsPathRooted("notes.txt"));
}
}
Output
True
notes.txt
False
How It Works
No file is touched. Both lines only work with text. The file notes.txt does not need to exist.
Two ways, one answer.Path.GetFullPath adds the working directory in front of a relative name. Path.Combine(cwd, ...) does it by hand. They give the same text, so the comparison is True.
The name stays the same. Only the folder part was added, so the last part is still notes.txt.
A relative path is not rooted."notes.txt" has no drive or leading slash, so IsPathRooted is False. This is why it needs a working directory.
Example 3 — Build a Path Under the Working Directory
Combine the folder with a name, then take the pieces apart again.
C#
using System;
using System.IO;
class Program {
static void Main() {
string cwd = Directory.GetCurrentDirectory();
string reports = Path.Combine(cwd, "reports");
Console.WriteLine(Directory.GetCurrentDirectory() == Environment.CurrentDirectory);
Console.WriteLine(Path.GetFileName(reports));
Console.WriteLine(Path.GetDirectoryName(reports) == cwd);
Console.WriteLine(reports.StartsWith(cwd));
}
}
Output
True
reports
True
True
How It Works
Two spellings, one value.Environment.CurrentDirectory reads the same setting, so the comparison with GetCurrentDirectory() is True.
Only text is built.Path.Combine joins the working directory and reports with the right separator. No folder is created.
Take it apart.GetFileName gives reports, and GetDirectoryName gives back the working directory, so the third line is True.
Still under the working directory.StartsWith(cwd) is True because the new path begins with the working directory. Real code would call CreateDirectory(reports) next if it needs the folder.
Frequently Asked Questions
The absolute path of the current working directory of the process. Relative paths, such as "data.txt", are resolved from this folder.
Not always. It depends on how the program was started. Use AppContext.BaseDirectory when you need the folder that holds your program files.
They return the same value. Environment.CurrentDirectory is a property you can also set, and Directory.GetCurrentDirectory() is the method form.
Because it differs on every computer. It is better to print facts such as Path.IsPathRooted(cwd), or only Path.GetFileName(cwd), and say that it varies.
Use Directory.SetCurrentDirectory(path). It changes the working directory for the whole program.