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,204
  • 14
  • 73
  • 127

3 Answers3

18

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
  • 107,182
  • 23
  • 252
  • 300
  • 4
    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,821
  • 8
  • 48
  • 77
1

Here is my suggestion:

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