Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions FileSystem/PathCombine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ public static string UniversalCombine(string[] paths, HashSet<char> invalidChars
}

List<string> nicePaths = new List<string>();
bool startsWithShare = false;

foreach (var path in paths)
{
var splitPath = path.Split(new[] { WindowsSeparator, LinuxSeparator }, StringSplitOptions.RemoveEmptyEntries);
string cleanPath = path;
if(path.StartsWith(WindowsSeparator+ WindowsSeparator))
{
cleanPath = path.TrimStart('\\');
startsWithShare = true;
}

var splitPath = cleanPath.Split(new[] { WindowsSeparator, LinuxSeparator }, StringSplitOptions.RemoveEmptyEntries);
nicePaths.AddRange(splitPath.Where(p => !String.IsNullOrWhiteSpace(p)));
}

Expand All @@ -36,7 +45,7 @@ public static string UniversalCombine(string[] paths, HashSet<char> invalidChars
{
separator = LinuxSeparator;
}
else if (nicePaths[0].EndsWith(":"))
else if (nicePaths[0].EndsWith(":") || startsWithShare)
{
separator = WindowsSeparator;
}
Expand All @@ -47,6 +56,11 @@ public static string UniversalCombine(string[] paths, HashSet<char> invalidChars

var result = String.Join(separator, nicePaths);

if (startsWithShare)
{
result = "\\\\" + result;
}

if (separator == LinuxSeparator && paths[0].StartsWith(LinuxSeparator))
{
// Linux paths need to start with a / only if we already had a /.
Expand Down
1 change: 1 addition & 0 deletions FileSystemTests/PathCombineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class PathCombineTests
[DataRow(new[] { "/github/", "FolderC/FolderD", "File.xml" }, "/github/FolderC/FolderD/File.xml")]
[DataRow(new[] { "/github", "FolderC/FolderD" }, "/github/FolderC/FolderD")]
[DataRow(new[] { "/github\\what?", "FolderC/FolderD" }, "/github/what?/FolderC/FolderD")]
[DataRow(new[] { "\\\\127.0.0.1\\c$", "skyline dataminer\\test.txt" }, "\\\\127.0.0.1\\c$\\skyline dataminer\\test.txt")]
public void UniversalCombineTests(string[] paths, string expectedResult)
{
var result = PathCombine.UniversalCombine(paths, _invalidCharacters);
Expand Down