示例#1
0
        public static string GetCommonPathPrefix(string path1, string path2)
        {
            Validate.IsNotNull(path1, "path1");
            Validate.IsNotNull(path2, "path2");
            if (path1.Length == 0 || path2.Length == 0)
            {
                return(string.Empty);
            }
            if (ArePathsEqual(path1, path2))
            {
                return(path1);
            }
            bool flag = path1.StartsWith("\\\\");

            if (flag != path2.StartsWith("\\\\"))
            {
                return(string.Empty);
            }
            using (ReusableResourceHolder <StringBuilder> reusableResourceHolder = ReusableStringBuilder.AcquireDefault(260))
            {
                StringBuilder resource   = reusableResourceHolder.Resource;
                PathParser    pathParser = new PathParser(path1);
                PathParser    other      = new PathParser(path2);
                while (pathParser.MoveNext() && other.MoveNext() && (pathParser.CurrentLength == other.CurrentLength && pathParser.CompareCurrentSegment(other) == 0))
                {
                    if (resource.Length == 0 & flag)
                    {
                        resource.Append("\\\\");
                    }
                    else if (resource.Length > 0)
                    {
                        resource.Append(Path.DirectorySeparatorChar);
                    }
                    resource.Append(path1, pathParser.CurrentStartIndex, pathParser.CurrentLength);
                }
                if (resource.Length == 2 && resource[1] == Path.VolumeSeparatorChar)
                {
                    resource.Append('\\');
                }
                return(resource.ToString());
            }
        }