How can I obtain the case-sensitive path on Windows?

后端 未结 8 649
慢半拍i
慢半拍i 2020-12-01 18:00

I need to know which is the real path of a given path.

For example:

The real path is: d:\\src\\File.txt
And the user give me: D:\\src\\file.txt
I nee

8条回答
  •  隐瞒了意图╮
    2020-12-01 18:33

    I tried to avoid dll imports so the best way for me was to use System.Linq and the System.IO.Directory class.

    For your example Real path is: d:\src\File.txt The user give me: D:\src\file.txt

    Code for this:

    using System.Linq;

    public static class PathUtils
    {
        public static string RealPath(string inputPath)
        {
            return Directory.GetFiles(Path.GetDirectoryName(inputPath))
                .FirstOrDefault(p => String.Equals(Path.GetFileName(p), 
                    Path.GetFileName(inputPath), StringComparison.OrdinalIgnoreCase));
        }
    }
    

    var p = PathUtils.RealPath(@"D:\src\file.txt");

    Method should return the path "d:\src\File.txt" or "D:\src\File.txt".

提交回复
热议问题