How do I resolve a canonical filename in Windows?

前端 未结 8 1416
自闭症患者
自闭症患者 2020-12-29 13:18

If I have a string that resolves to a file path in Windows, is there an accepted way to get a canonical form of the file name?

For example, I\'d like to know whether

8条回答
  •  情书的邮戳
    2020-12-29 13:31

    Using FileInfo (example in C#):

    FileInfo info1 = new FileInfo(@"C:\stuff\things\etc\misc\whatever.txt");
    FileInfo info2 = new FileInfo(@"C:\stuff\things\etc\misc\other\..\whatever.txt");
    if (info1.FullName.Equals(info2.FullName)) {
        Console.WriteLine("yep, they're equal");
    }
    Console.WriteLine(info1.FullName);
    Console.WriteLine(info2.FullName);
    

    Output is:

    yep, they're equal
    C:\stuff\things\etc\misc\whatever.txt
    C:\stuff\things\etc\misc\whatever.txt

提交回复
热议问题