简体   繁体   English

PathRelativePathTo正在将Unicode字符转换为ASCII

[英]PathRelativePathTo is transforming unicode charactrs to ascii

I have the following function: 我有以下功能:

    public static string GetRelativePath(string fromPath, string toPath)
    {
        // we also tried the Uri solution but that does not return .. when you need to traverse
        // one up only
        // uri1 = "bla\foo"
        // uri2 = "bla\bar"
        // uri1.MakeRelaitveto(uri2) != "..\bar"
        var path = new StringBuilder(260); // MAX_PATH
        if (PathRelativePathTo(
            path,
            fromPath.Replace('/', '\\'),
            FILE_ATTRIBUTE_DIRECTORY,
            toPath.Replace('/', '\\'),
            FILE_ATTRIBUTE_DIRECTORY) == 0)
        {
            return toPath;
        }

        return path.ToString().Replace('\\', Path.DirectorySeparatorChar);
    }

    [DllImport("shlwapi.dll", SetLastError = true)]
    private static extern int PathRelativePathTo(
        StringBuilder pszPath, string pszFrom, int dwAttrFrom, string pszTo, int dwAttrTo);
}

Which I call with the following testcase: 我用以下测试用例调用它:

GetPathRelativeTo("C:\\somεpath", "C:\\anothεrpath").Shouldbe("..\\anothεrpath")

but instead it is returning ..\\\\anotherpath . 但相反,它返回..\\\\anotherpath Notice that the ε has been replaced by an e . 注意, ε已被e替换。

I tried using PathRelativePathToW but that works even less (other test cases fail). 我尝试使用PathRelativePathToW但是效果更差(其他测试用例失败)。

Does anybody know what is going on and how I can prevent the replacement of the char? 有谁知道发生了什么事以及如何防止替换char?

This seems to be a problem with shlwapi under the hood, not anything on the C# side. 这似乎是shlwapi的问题所在,而不是C#方面的任何问题。

Consider using System.Uri instead. 考虑改用System.Uri

How do I get a relative path from one path to another in C# 如何在C#中获得从一条路径到另一条路径的相对路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM