How do I get .NET's Path.Combine to convert forward slashes to backslashes?

C#.Net

C# Problem Overview


I'm using Path.Combine like so:

Path.Combine("test1/test2", "test3\\test4");

The output I get is:

test1/test2\test3\test4

Notice how the forward slash doesn't get converted to a backslash. I know I can do string.Replace to change it, but is there a better way of doing this?

C# Solutions


Solution 1 - C#

As others have said, Path.Combine doesn't change the separator. However if you convert it to a full path:

Path.GetFullPath(Path.Combine("test1/test2", "test3\\test4"))

the resulting fully qualified path will use the standard directory separator (backslash for Windows).

Note that this works on Windows because both \ and / are legal path separators:

Path.DirectorySeparatorChar = \
Path.AltDirectorySeparatorChar = /

If you run on, say, .NET Core 2.0 on Linux, only the forward slash is a legal path separator:

Path.DirectorySeparatorChar = /
Path.AltDirectorySeparatorChar = /

and in this case it won't convert backslash to forward slash, because backslash is not a legal alternate path separator.

Solution 2 - C#

Because your "test1/test2" is already a string literal, Path.Combine will not change the '/' for you to a ''.

Path.Combine will only concat the 2 string literals with the appropriate path delimiter used by the OS, in this case Windows, which is '', from there your output

test1/test2\test3\test4

Your best bet would be the string.Replace.

Solution 3 - C#

Try using the Uri class. It will create valid Uris for the correct target machine (/ -> \).

Solution 4 - C#

First, I would argue in this particular case, it wouldn't be unreasonable to do a single .Replace()

Secondly, you could also use System.Uri to format your path, it's very strict. However, this will be more lines than a simple .Replace(). I apperently am voting for you to just use .Replace() be done with it! Hope that helps

Solution 5 - C#

Using .NET Reflector, you can see that Path.Combine doesn't change slashes in the provided strings

public static string Combine(string path1, string path2)
{
    if ((path1 == null) || (path2 == null))
    {
        throw new ArgumentNullException((path1 == null) ? "path1" : "path2");
    }
    CheckInvalidPathChars(path1);
    CheckInvalidPathChars(path2);
    if (path2.Length == 0)
    {
        return path1;
    }
    if (path1.Length == 0)
    {
        return path2;
    }
    if (IsPathRooted(path2))
    {
        return path2;
    }
    char ch = path1[path1.Length - 1];
    if (((ch != DirectorySeparatorChar) && (ch != AltDirectorySeparatorChar)) && (ch != VolumeSeparatorChar))
    {
        return (path1 + DirectorySeparatorChar + path2);
    }
    return (path1 + path2);
}

You can do the same with String.Replace and the Uri class methods to determine which one works best for you.

Solution 6 - C#

If you need your result to have forward slashes instead of backward slashes, and if your first path component is absolute (i.e. rooted) path, you could actually combine it using the Uri class:

string CombinedPath = new Uri(new Uri("C:/test1/test2"), "test3\\test4").AbsolutePath;

Note that this won't work if the first component is relative path too.

Solution 7 - C#

Try using

var fullname = new DirectoryInfo(Path.Combine("f:/","test1/test2", "test3\\test4")).FullName;

This will result in

f:\test1\test2\test3\test4

Solution 8 - C#

No, the seperator character is defined as a Read Only.

http://msdn.microsoft.com/en-us/library/system.io.path.pathseparator.aspx

You should use a Replace as it's a trivial change.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel T.View Question on Stackoverflow
Solution 1 - C#JoeView Answer on Stackoverflow
Solution 2 - C#RiaanView Answer on Stackoverflow
Solution 3 - C#TimView Answer on Stackoverflow
Solution 4 - C#Robert SederView Answer on Stackoverflow
Solution 5 - C#GaTechThomasView Answer on Stackoverflow
Solution 6 - C#Igor LevickiView Answer on Stackoverflow
Solution 7 - C#Andrew CoatsView Answer on Stackoverflow
Solution 8 - C#Keith AdlerView Answer on Stackoverflow