0

I’m developing an application that must read an Input file, which contains several file paths, and then open and read each one of them. Let’s suppose that the Input file is in the following path:

C:\temp\Analysis\InputFile\Input.txt

The paths that are in Input.txt are relative, as follows:

.\Config.txt
.\TestFile\Test.txt
..\Materials.txt
..\PropertyFile\Properties.txt
..\..\AnalysisManagement.txt

I would like to know how to pass these relative paths to C# in order to it correctly understand the path. Need I to write a routine to convert these relative paths to absolute paths or there is a method to work with relative paths in C#?

  • 1
    They should be relative to what? – Cid Dec 01 '20 at 13:15
  • If they are relative to the current working directory, you can use [`Path.GetFullPath()`](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfullpath?view=net-5.0) – Matthew Watson Dec 01 '20 at 13:16
  • @MatthewWatson current working directory isn't necessarily the executable directory. – Cid Dec 01 '20 at 13:17
  • 1
    If they are relative to the currently executing program, you can use `Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)` – Robert Harvey Dec 01 '20 at 13:18
  • https://dotnetfiddle.net/5uPeXI <-- I think you are looking for this ... – Fildor Dec 01 '20 at 13:29
  • .Net Core has [a new overload for `Path.GetFullPath()`](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfullpath?view=net-5.0#System_IO_Path_GetFullPath_System_String_System_String_) that helps with this. Check it out. – Matthew Watson Dec 01 '20 at 13:43

0 Answers0