0

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:

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
mikolaj semeniuk
  • 2,030
  • 15
  • 31
  • Those methods look to me to be doing what they should be. In theory you *could* have a directory `/Users/user-name/Projects/Users/user-name/Projects`, although I'm guessing you don't. Please edit your question to include the expected output. – Luke Woodward Jan 16 '22 at 10:59
  • 1
    The absolute path of a relative path depends on the current work directory of the application. If you don't change the current work directory of an application, then it will be the starting directory where you executed the application. What absolute path do you expect from the different locations where you can start your application? Why do you expect the absolute path would be just the same but with a `/` at the front? What exactly are you trying to do with the relative and absolute path? – Progman Jan 16 '22 at 14:34

0 Answers0