Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d644d8a

Browse files
committed
Fix handling of generating relative path to parent (#18460)
Fixes #30263
1 parent ed65cca commit d644d8a

File tree

1 file changed

+16
-6
lines changed
  • src/mscorlib/shared/System/IO

1 file changed

+16
-6
lines changed

src/mscorlib/shared/System/IO/Path.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,14 @@ private static string GetRelativePath(string relativeTo, string path, StringComp
731731
// Add parent segments for segments past the common on the "from" path
732732
if (commonLength < relativeToLength)
733733
{
734-
sb.Append(PathInternal.ParentDirectoryPrefix);
734+
sb.Append("..");
735735

736-
for (int i = commonLength; i < relativeToLength; i++)
736+
for (int i = commonLength + 1; i < relativeToLength; i++)
737737
{
738738
if (PathInternal.IsDirectorySeparator(relativeTo[i]))
739739
{
740-
sb.Append(PathInternal.ParentDirectoryPrefix);
740+
sb.Append(DirectorySeparatorChar);
741+
sb.Append("..");
741742
}
742743
}
743744
}
@@ -749,11 +750,20 @@ private static string GetRelativePath(string relativeTo, string path, StringComp
749750
}
750751

751752
// Now add the rest of the "to" path, adding back the trailing separator
752-
int count = pathLength - commonLength;
753+
int differenceLength = pathLength - commonLength;
753754
if (pathEndsInSeparator)
754-
count++;
755+
differenceLength++;
756+
757+
if (differenceLength > 0)
758+
{
759+
if (sb.Length > 0)
760+
{
761+
sb.Append(DirectorySeparatorChar);
762+
}
763+
764+
sb.Append(path, commonLength, differenceLength);
765+
}
755766

756-
sb.Append(path, commonLength, count);
757767
return StringBuilderCache.GetStringAndRelease(sb);
758768
}
759769

0 commit comments

Comments
 (0)