示例#1
0
        public static bool TryGetCommonRootDirectory(List <DirectoryPathAbsolute> listOfPaths, out DirectoryPathAbsolute commonRootDirectory)
        {
            commonRootDirectory = DirectoryPathAbsolute.Empty;
            if (listOfPaths == null)
            {
                return(false);
            }
            if (listOfPaths.Count == 0)
            {
                return(false);
            }

            // If the list contains a path null or empty -> no commonRootDirectory
            foreach (DirectoryPathAbsolute path in listOfPaths)
            {
                if (PathHelper.IsNullOrEmpty(path))
                {
                    return(false);
                }
            }

            if (listOfPaths.Count == 1)
            {
                commonRootDirectory = listOfPaths[0];
                return(true);
            }


            //
            //  Case where all paths are identical
            //
            bool allPathsAreIdentical = true;

            for (int i = 1; i < listOfPaths.Count; i++)
            {
                if (!listOfPaths[0].Equals(listOfPaths[i]))
                {
                    allPathsAreIdentical = false;
                    break;
                }
            }
            if (allPathsAreIdentical)
            {
                commonRootDirectory = listOfPaths[0];
                return(true);
            }

            //
            //  Case where some paths are not on the same drive
            //
            for (int i = 1; i < listOfPaths.Count; i++)
            {
                if (string.Compare(listOfPaths[0].Drive, listOfPaths[i].Drive, true) != 0)
                {
                    return(false);
                }
            }

            //
            //  Build listOfSplittedPaths
            //
            List <string[]> listOfSplittedPaths = new List <string[]>();
            int             maxDeepForRootDir   = int.MaxValue;

            foreach (DirectoryPathAbsolute path in listOfPaths)
            {
                string[] pathSplitted = path.Path.Split(new char[] { Path.DirectorySeparatorChar });
                if (pathSplitted.Length < maxDeepForRootDir)
                {
                    maxDeepForRootDir = pathSplitted.Length;
                }
                listOfSplittedPaths.Add(pathSplitted);
            }


            //
            // Compute commonRootDirPath from listOfSplittedPaths
            //
            string commonRootDirPath = string.Empty;

            for (int i = 0; i < maxDeepForRootDir; i++)
            {
                string current = listOfSplittedPaths[0][i];
                foreach (string[] pathSplitted in listOfSplittedPaths)
                {
                    if (string.Compare(pathSplitted[i], current, true) != 0)
                    {
                        commonRootDirectory = new DirectoryPathAbsolute(commonRootDirPath);
                        return(true);
                    }
                }
                if (i == 0)
                {
                    commonRootDirPath += current;
                }
                else
                {
                    commonRootDirPath += Path.DirectorySeparatorChar + current;
                }
            }
            commonRootDirectory = new DirectoryPathAbsolute(commonRootDirPath);
            return(true);
        }