Example #1
0
        /// <summary>
        /// Check if a directory is a child of the parent
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="child"></param>
        /// <returns></returns>
        public static bool IsSubDirectory(DirectoryInfo parent, DirectoryInfo child)
        {
            #region Sanity Check
            if (null == parent)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (null == child)
            {
                throw new ArgumentNullException(nameof(child));
            }
            #endregion

            #region Fast Checks
            // "c:\foo\bar" is a child of "c:\foo"
            var pFullName = DirectoryInfoComparer.FullName(parent);
            var cFullName = DirectoryInfoComparer.FullName(child);

            // if the parent is longer than the child
            // then it cannot posibly be a child.
            if (pFullName.Length > cFullName.Length)
            {
                return(false);
            }

            // are they equal?
            if (pFullName.Length == cFullName.Length)
            {
                return(Equals(parent, child));
            }

            // if the root paths are not the same... then don't do it.
            if (string.Compare(Path.GetPathRoot(pFullName), Path.GetPathRoot(cFullName), StringComparison.OrdinalIgnoreCase) != 0)
            {
                return(false);
            }
            #endregion

            //  is the child a substring of the parent...
            return(cFullName.ToUpper().Contains(pFullName.ToUpper()));
        }
Example #2
0
 public void FullNameCaseAreKept(string given, string expected)
 {
     Assert.AreEqual(expected, DirectoryInfoComparer.FullName(new DirectoryInfo(given)));
 }
Example #3
0
 public void FullNameIsProperlyChanged(string lhs, string rhs)
 {
     Assert.AreEqual(
         DirectoryInfoComparer.FullName(new DirectoryInfo(lhs)), rhs);
 }
Example #4
0
 public void FulleNameCannotBeNull()
 {
     Assert.Throws <ArgumentNullException>(() => DirectoryInfoComparer.FullName(null));
 }