I try to get absolute from relative path but
- it has to work on any OS
- it has to work no matter if we run application from project directory or different location (for instance using
dotnet run --project PROJECT_NAME
)
I tried to find answers in previous posts like:
- Relative path to absolute path in C#?
- How to convert a relative path to an absolute path in a Windows application?
but none of them worked for me.
This is what I already tried:
var relative_path_to_file = "Users/user_name/Projects/Application/output.json";
Console.WriteLine($"InputPath:\n\t{relative_path_to_file}");
Console.WriteLine($"Path.FullPath:\n\t{Path.GetFullPath(relative_path_to_file)}");
Console.WriteLine($"Path.GetDirectoryName + Path.GetFullPath:\n\t{Path.GetDirectoryName(Path.GetFullPath(relative_path_to_file))}");
and then run
# current directory: Users/user_name/Projects
dotnet run --project application
output
InputPath:
Users/user_name/Projects/Application/output.json
Path.FullPath:
/Users/user_name/Projects/Users/user_name/Projects/Application/output.json
Path.GetDirectoryName + Path.GetFullPath:
/Users/user_name/Projects/Users/user_name/Projects/Application
Expected output:
ExpectedPath:
/Users/user_name/Projects/Application/output.json