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

后端 未结 8 645
慢半拍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:19

    Alternative Solution

    Here is a solution that worked for me to move files between Windows and a server using case sensitive paths. It walks down the directory tree and corrects each entry with GetFileSystemEntries(). If part of the path is invalid (UNC or folder name), then it corrects the path only up to that point and then uses the original path for what it can't find. Anyway, hopefully this will save others time when dealing with the same issue.

    private string GetCaseSensitivePath(string path)
    {
        var root = Path.GetPathRoot(path);
        try
        {
            foreach (var name in path.Substring(root.Length).Split(Path.DirectorySeparatorChar))
                root = Directory.GetFileSystemEntries(root, name).First();
        }
        catch (Exception e)
        {
            // Log("Path not found: " + path);
            root += path.Substring(root.Length);
        }
        return root;
    }
    

提交回复
热议问题