public AllorsDirectoryInfo(DirectoryInfo directoryInfo) { this.directoryInfo = directoryInfo; if (directoryInfo.Parent != null) { this.parent = new AllorsDirectoryInfo(directoryInfo.Parent); } }
private AllorsDirectoryInfo GetCommonAncestor(AllorsDirectoryInfo destination) { if (destination.IsAncestor(this)) { return(this); } return(this.parent.GetCommonAncestor(destination)); }
private void BuildAncestors(AllorsDirectoryInfo root, List <AllorsDirectoryInfo> ancestors) { if (!this.directoryInfo.FullName.Equals(this.directoryInfo.Root.FullName)) { if (!this.directoryInfo.FullName.Equals(root.directoryInfo.FullName)) { ancestors.Add(this); this.parent.BuildAncestors(root, ancestors); } } }
public string GetRelativeName(DirectoryInfo baseDirectoryInfo) { var baseDirectory = new AllorsDirectoryInfo(baseDirectoryInfo); var directory = new AllorsDirectoryInfo(fileInfo.Directory); string relativePath = directory.GetRelativeName(baseDirectory); if (relativePath == null) { return(null); } return(Path.Combine(relativePath, this.fileInfo.Name)); }
private bool IsAncestor(AllorsDirectoryInfo ancestor) { if (this.directoryInfo.FullName.Equals(ancestor.directoryInfo.FullName)) { return(true); } if (this.parent != null) { return(this.parent.IsAncestor(ancestor)); } return(false); }
internal string GetRelativeName(AllorsDirectoryInfo baseDirectory) { if (this.directoryInfo.Root.FullName.Equals(baseDirectory.directoryInfo.Root.FullName)) { AllorsDirectoryInfo commonAncestor = this.GetCommonAncestor(baseDirectory); var ancestors = new List <AllorsDirectoryInfo>(); this.BuildAncestors(commonAncestor, ancestors); var baseAncestors = new List <AllorsDirectoryInfo>(); baseDirectory.BuildAncestors(commonAncestor, baseAncestors); var relativePath = new StringBuilder(); foreach (AllorsDirectoryInfo baseAncestor in baseAncestors) { if (relativePath.Length > 0) { relativePath.Append(Path.DirectorySeparatorChar); } relativePath.Append(".."); } for (int i = ancestors.Count - 1; i >= 0; --i) { AllorsDirectoryInfo ancestor = ancestors[i]; if (relativePath.Length > 0) { relativePath.Append(Path.DirectorySeparatorChar); } relativePath.Append(ancestor.directoryInfo.Name); } return(relativePath.ToString()); } return(null); }