c# Relative Path convert to Absolute Path

时间:2022-09-11 21:46:14

Reference:
http://*.com/questions/4796254/relative-path-to-absolute-path-in-c
http://*.com/questions/1399008/how-to-convert-a-relative-path-to-an-absolute-path-in-a-windows-application

Relative Path => Absolute Path

  var path = Path.Combine(FolderPath, relative);
  path = Path.GetFullPath(path);

Absolute => Relative Path

   var fileUri = new Uri(strTargetPath);
   var refUri = new Uri(Application.ExecutablePath);
   var strRelative = refUri.MakeRelativeUri(fileUri).ToString();
   strRelative = Uri.UnescapeDataString(strRelative);

You should use Uri.UnescapteDataString to convert special character such as %23 to “#”.
Here is the reference:
http://*.com/questions/575440/url-encoding-using-c-sharp