/// <summary> /// Returns whether the current PathInfo is a valid parent of the child path info /// passed as argument. /// </summary> /// <param name="child">The path info to verify</param> /// <returns>Whether it is true that the current path info is a parent of child.</returns> /// <exception cref="NotSupportedException">If this instance of path info and child aren't rooted.</exception> public bool IsParentOf(PathInfo child) { if (Root == string.Empty || child.Root == string.Empty) throw new NotSupportedException("Non-rooted paths are not supported."); var OK = child.FolderAndFiles.StartsWith(FolderAndFiles); switch (Type) { case PathType.Device: OK &= child.DeviceName.ToLowerInvariant() == DeviceName.ToLowerInvariant(); break; case PathType.Server: OK &= child.ServerName.ToLowerInvariant() == ServerName.ToLowerInvariant(); break; case PathType.IPv4: OK &= IPAddress.Parse(child.IPv4).Equals(IPAddress.Parse(IPv4)); break; case PathType.IPv6: OK &= (IPAddress.Parse(child.IPv6).Equals(IPAddress.Parse(IPv6))); break; case PathType.Relative: throw new InvalidOperationException("Since root isn't empty we should never get relative paths."); case PathType.Drive: OK &= DriveLetter.ToLowerInvariant() == child.DriveLetter.ToLowerInvariant(); break; } return OK; }
/// <summary> /// Removes the path info passes as a parameter from the current root. Only works for two rooted paths with same root. /// Does NOT cover all edge cases, please verify its intended results yourself. /// <example> /// /// </example> /// </summary> /// <param name="other"></param> /// <returns></returns> public string RemoveParameterFromRoot(PathInfo other) { if (Root != other.Root) throw new InvalidOperationException("Roots of this and other don't match."); if (other.FolderAndFiles.Length > FolderAndFiles.Length) throw new InvalidOperationException( "The folders and files part of the second parameter must be shorter than that path you wish to subtract from."); if (other.FolderAndFiles == FolderAndFiles) return string.Empty; return FolderAndFiles.Substring(other.FolderAndFiles.Length).TrimStart(Path.GetDirectorySeparatorChars()); }
/// <summary> /// Removes the path info passes as a parameter from the current root. Only works for two rooted paths with same root. /// Does NOT cover all edge cases, please verify its intended results yourself. /// <example> /// </example> /// </summary> /// <param name = "other"></param> /// <returns></returns> public string RemoveParameterFromRoot(PathInfo other) { Contract.Requires(Root == other.Root, "roots must match to be able to subtract"); Contract.Requires(FolderAndFiles.Length >= other.FolderAndFiles.Length, "The folders and files part of the parameter must be shorter or equal to in length, than that path you wish to subtract from."); if (other.FolderAndFiles == FolderAndFiles) return string.Empty; var startIndex = other.FolderAndFiles.Length; Contract.Assume(startIndex <= FolderAndFiles.Length); var substring = FolderAndFiles.Substring(startIndex); return substring.TrimStart(Path.GetDirectorySeparatorChars()); }