Skip to content

path2.RelativeTo(path1) doesn't work if path2 isn't inside path1 #19

@oxygen-dioxide

Description

@oxygen-dioxide

In System.IO, Path.GetRelativePath(pathstr1, pathstr2) doesn't require that pathstr2 is inside pathstr1

using System.IO;
string pathstr1 = "E:/a";
string pathstr2 = "E:/a/b";
Console.WriteLine(Path.GetRelativePath(pathstr1, pathstr2)); // "b" 
Console.WriteLine(Path.GetRelativePath(pathstr1, pathstr1)); // "."
Console.WriteLine(Path.GetRelativePath(pathstr2, pathstr1)); // ".."

However, In PathLib, running path2.RelativeTo(path1) when path2 isn't inside path1 will throw an error:

using PathLib;
IPath path1 = Paths.Create("E:/a");
IPath path2 = Paths.Create("E:/a/b");

Console.WriteLine(path2.RelativeTo(path1)); // "b" 
Console.WriteLine(path1.RelativeTo(path1)); // error
Console.WriteLine(path1.RelativeTo(path2)); // error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions