How to get the folder path from a full file path in C#?
Hello everyone! I'm a beginner in C# and .NET, writing a small application for file processing. I've encountered this problem: I have a string containing the full path to a file, for example:
```csharp
string fullPath = @"C:\Users\Иван\Documents\Отчеты\report_2024.xlsx";
```
I need to get only the directory from this path, i.e., `C:\Users\Иван\Documents\Отчеты\`, so that later I can, for example, save another file there or check if the folder exists.
I tried simply using `Substring` and looking for the last slash, but this seems hacky and unreliable — what if the path contains Cyrillic characters or different separators? Plus on Linux the slashes are different.
Please tell me what is the correct way in .NET to extract the directory from a full file path? I want it to work
No answers yet.
Log in to leave an answer
Blogs