How to get the current working folder in C#
By FoxLearn 11/13/2024 4:07:13 PM 466
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
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#
Categories
Popular Posts
Stisla Admin Dashboard Template
11/18/2024
Admin Tailwind CSS Admin Dashboard Template
11/18/2024
Portal HTML Bootstrap
11/14/2024