public void Test_GetRelativePathWithError3() { IAbsoluteDirectoryPath absoluteDirectoryPathTo = @"C:\Dir1".ToAbsoluteDirectoryPath(); IAbsoluteDirectoryPath absoluteDirectoryPathFrom = @"D:\Dir1".ToAbsoluteDirectoryPath(); Assert.IsFalse(absoluteDirectoryPathTo.CanGetRelativePathFrom(absoluteDirectoryPathFrom)); string failureReason; Assert.IsFalse(absoluteDirectoryPathTo.CanGetRelativePathFrom(absoluteDirectoryPathFrom, out failureReason)); Assert.IsTrue(failureReason == @"Cannot compute relative path from 2 paths that are not on the same volume PathFrom = ""D:\Dir1"" PathTo = ""C:\Dir1"""); Assert.Throws(typeof(ArgumentException), delegate { absoluteDirectoryPathTo.GetRelativePathFrom(absoluteDirectoryPathFrom); }); }
public void Test_GetRelativePathWithError5() { IAbsoluteDirectoryPath absoluteDirectoryPathTo = @"\\Server1\Share".ToAbsoluteDirectoryPath(); IAbsoluteDirectoryPath absoluteDirectoryPathFrom = @"\\Server2\Share".ToAbsoluteDirectoryPath(); Assert.IsFalse(absoluteDirectoryPathTo.CanGetRelativePathFrom(absoluteDirectoryPathFrom)); Assert.Throws(typeof(ArgumentException), delegate { absoluteDirectoryPathTo.GetRelativePathFrom(absoluteDirectoryPathFrom); }); }
public void Test_GetRelativePathWithError5() { IAbsoluteDirectoryPath absoluteDirectoryPathTo = @"\\Server1\Share".ToAbsoluteDirectoryPath(); IAbsoluteDirectoryPath absoluteDirectoryPathFrom = @"\\Server2\Share".ToAbsoluteDirectoryPath(); Assert.IsFalse(absoluteDirectoryPathTo.CanGetRelativePathFrom(absoluteDirectoryPathFrom)); absoluteDirectoryPathTo.GetRelativePathFrom(absoluteDirectoryPathFrom); }
public static string GetRelativeDirectory(this IAbsoluteDirectoryPath path, IAbsoluteDirectoryPath possibleRoot) { if (path.Equals(possibleRoot) || !path.CanGetRelativePathFrom(possibleRoot) || !path.IsRootedIn(possibleRoot)) { return(path.ToString()); } return(path.GetRelativePathFrom(possibleRoot).Join()); }