Relative Path to Absolute Path in C#

Relative path to absolute path in C#?

Assuming you know the real directory the XML file lives in use Path.Combine, e.g.

var absolute_path = Path.Combine(directoryXmlLivesIn, "..\images\image.jpg");

If you want to get back the full path with any ..'s collapsed then you can use:

Path.GetFullPath((new Uri(absolute_path)).LocalPath);

How to convert a relative path to an absolute path in a Windows application?

Have you tried:

string absolute = Path.GetFullPath(relative);

? Note that that will use the current working directory of the process, not the directory containing the executable. If that doesn't help, please clarify your question.

Get absolute path from relative path c#

You can use the static methods of Path to calculate the resulting path:

string fullPathToSecondFile = "c:\\test\\subtestsecond\\secondfile.txt";
string relativePath = "..\\subtestfirst\\firstfile.txt";

string fullPathToFirstFile = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(fullSecondPath), relativePath));

This results in c:\test\subtestfirst\firstfile.txt


What happens is that you combine a relative path to a absolute one. This results in c:\test\subtestsecond\..\subtestfirst\firstfile.txt.

In the second step Path.GetFullPath() normalizes the string to the result shown above.

Absolute to Relative path

The Uri class has a MakeRelativeUri method that can help.

public static string MakeRelative(string filePath, string referencePath)
{
var fileUri = new Uri(filePath);
var referenceUri = new Uri(referencePath);
return Uri.UnescapeDataString(referenceUri.MakeRelativeUri(fileUri).ToString()).Replace('/', Path.DirectorySeparatorChar);
}

var result = MakeRelative(@"C:\dirName\dirName2\file.txt", @"C:\dirName\");

Convert Relative Path to Absolute Path

You need to use Path.GetFullPath() to have the upper directory "../../" taken into account, see below :

string exeFile = new System.Uri(Assembly.GetEntryAssembly().CodeBase).AbsolutePath;
string Dir = Path.GetDirectoryName(exeFile);
string path = Path.GetFullPath(Path.Combine(Dir, @"..\..\Help\Help.txt"));
System.Diagnostics.Process.Start(path);

Per the MSDN of GetFullPath : Returns the absolute path for the specified path string.
Whereas Path.Combine Combines strings into a path.

What is the difference between an absolute and a relative path?

Say you were giving directions to a spot. You have two methods you can describe getting to the location:

  • Relative to where you stand,
  • Relative to a landmark.

Both get you to the same location, but the former doesn't always work ("take a left, then a right, go through two lights then take another right" wouldn't necessarily work from the next town over, but works from where you stand). That's essentially the difference.

If you have C:\Windows\System32, that's an absolute path. If you have Windows\System32, it will only work so long as you're starting from C:\. If you start in C:\Program Files you would need a ..\ to get there correctly.

However, no matter where you are on the hard drive, C:\Windows\System32\ is a definitive way to get to that folder.

Converting absolute path to relative path C#

Relative paths are based from the binary file from which your application is running. By default, your binary files will be outputted in the [directory of your .csproj]/bin/debug. So let's say you wanted to create your images folder at the same level as your .csproj. Then you could access your images using the relative path "../../images/someImage.jpg".

To get a better feel for this, try out the following as a test:

1) create a new visual studio sample project,

2) create an images folder at the same level as the .csproj

3) put some files in the images folder

4) put this sample code in your main method -

    static void Main(string[] args)
{
Console.WriteLine(Directory.GetCurrentDirectory());
foreach (string s in Directory.EnumerateFiles("../../images/"))
{
Console.WriteLine(s);
}
Console.ReadLine(); // Just to keep the console from disappearing.
}

You should see the relative paths of all the files you placed in step (3).



Related Topics



Leave a reply



Submit