How to get relative path from absolute path

前端 未结 23 2060
既然无缘
既然无缘 2020-11-22 11:52

There\'s a part in my apps that displays the file path loaded by the user through OpenFileDialog. It\'s taking up too much space to display the whole path, but I don\'t want

23条回答
  •  一整个雨季
    2020-11-22 12:43

    If you know that toPath is contained by fromPath then you can keep it simple. I'll leave out the asserts for brevity.

    public static string MakeRelativePath(string fromPath, string toPath)
    {
        // use Path.GetFullPath to canonicalise the paths (deal with multiple directory seperators, etc)
        return Path.GetFullPath(toPath).Substring(Path.GetFullPath(fromPath).Length + 1);
    }
    

提交回复
热议问题