簡體   English   中英

PathRelativePathTo正在將Unicode字符轉換為ASCII

[英]PathRelativePathTo is transforming unicode charactrs to ascii

我有以下功能:

    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);
}

我用以下測試用例調用它:

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

但相反,它返回..\\\\anotherpath 注意, ε已被e替換。

我嘗試使用PathRelativePathToW但是效果更差(其他測試用例失敗)。

有誰知道發生了什么事以及如何防止替換char?

這似乎是shlwapi的問題所在,而不是C#方面的任何問題。

考慮改用System.Uri

如何在C#中獲得從一條路徑到另一條路徑的相對路徑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM