How to get the current working folder in C#

By FoxLearn 11/13/2024 4:07:13 PM   4
In C#, you can get the current working directory using the Directory.GetCurrentDirectory() method, which is part of the System.IO namespace.

How to get the current working folder in C#?

You can use the static `Directory` class in C# to get the current working directory with `Directory.GetCurrentDirectory()`.

string currentDirectory = System.IO.Directory.GetCurrentDirectory();
Console.WriteLine("Current working directory: " + currentDirectory);

Directory.GetCurrentDirectory() returns the path of the directory that the process started from.

The working directory is not necessarily the same as the location of the executable. If you need the location of the actual executable file, you can use Assembly.GetExecutingAssembly().Location.

string executablePath = Assembly.GetExecutingAssembly().Location;
Console.WriteLine("Executable path: " + executablePath);

You can also use the following code to get the directory path of the application executable:

string exePath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);

You can also change the working directory using Directory.SetCurrentDirectory() if needed