10

How to get canonical file name by non-canonical one.

E.g. I want to call function which converts "C:\Program files\..\Windows\aaa.txt" to "C:\Windows\aaa.txt"

I am looking for something like Java File.getCanonicalPath()

sergtk
  • 10,714
  • 15
  • 75
  • 130

3 Answers3

19

You can use the Path.GetFullPath method for this.

Example:

Console.WriteLine(Path.GetFullPath(@"C:\Program files\..\Windows\aaa.txt"));

Output:

C:\Windows\aaa.txt

Ani
  • 111,048
  • 26
  • 262
  • 307
  • 5
    It's good but be warned it's not a full replacement of CanonicalPath - Java's CanonicalPath will do things like normalize the capitalization of drive letters inside the path, and that is not happening in .NET's GetFullPath... In .NET the `Path` class has a GetFullPath method and there is similar functionality in the FullName property of `FileInfo`. – Mishax Aug 22 '13 at 10:49
3
System.IO.Path.GetFullPath("C:/Program files/../Windows/aaa.txt")

will return

"C:\\Windows\\aaa.txt"
Goran Obradovic
  • 8,951
  • 9
  • 50
  • 79
1

Here is my suggestion:

string path = Path.GetFullPath(@"C:\Program files\..\Windows\aaa.txt");
Fischermaen
  • 12,238
  • 2
  • 39
  • 56