コード例 #1
0
 public AllorsDirectoryInfo(DirectoryInfo directoryInfo)
 {
     this.directoryInfo = directoryInfo;
     if (directoryInfo.Parent != null)
     {
         this.parent = new AllorsDirectoryInfo(directoryInfo.Parent);
     }
 }
コード例 #2
0
        private AllorsDirectoryInfo GetCommonAncestor(AllorsDirectoryInfo destination)
        {
            if (destination.IsAncestor(this))
            {
                return(this);
            }

            return(this.parent.GetCommonAncestor(destination));
        }
コード例 #3
0
 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);
         }
     }
 }
コード例 #4
0
        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));
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }