Why does Path.Combine not properly concatenate filenames that start with Path.DirectorySeparatorChar?
Asked Answered
B

16

229

From the Immediate Window in Visual Studio:

> Path.Combine(@"C:\x", "y")
"C:\\x\\y"
> Path.Combine(@"C:\x", @"\y")
"\\y"

It seems that they should both be the same.

The old FileSystemObject.BuildPath() didn't work this way...

Bandog answered 9/9, 2008 at 23:5 Comment(3)
It still doesn't change in .NET core.Emanuelemanuela
@Joe, stupid is right! Also, I must point out that the equivalent function works just fine in Node.JS ... Shaking my head at Microsoft...Corrigan
@Emanuelemanuela For .NET Core/Standard, Path.Combine() is mainly for backwards compatibility (with the existing behaviour). You'd be better off using Path.Join(): "Unlike the Combine method, the Join method does not attempt to root the returned path. (That is, if path2 is an absolute path, the Join method does not discard path1 and return path2 as the Combine method does.)"Pretoria
E
242

This is kind of a philosophical question (which perhaps only Microsoft can truly answer), since it's doing exactly what the documentation says.

System.IO.Path.Combine

"If path2 contains an absolute path, this method returns path2."

Here's the actual Combine method from the .NET source. You can see that it calls CombineNoChecks, which then calls IsPathRooted on path2 and returns that path if so:

public static String Combine(String path1, String path2) {
    if (path1==null || path2==null)
        throw new ArgumentNullException((path1==null) ? "path1" : "path2");
    Contract.EndContractBlock();
    CheckInvalidPathChars(path1);
    CheckInvalidPathChars(path2);

    return CombineNoChecks(path1, path2);
}

internal static string CombineNoChecks(string path1, string 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 + DirectorySeparatorCharAsString + path2;
    return path1 + path2;
}

I don't know what the rationale is. I guess the solution is to strip off (or Trim) DirectorySeparatorChar from the beginning of the second path; maybe write your own Combine method that does that and then calls Path.Combine().

Evanevander answered 9/9, 2008 at 23:16 Comment(6)
Looking at the disassembled code (check my post), you are right in a way.Cricket
I would guess it works that way to allow easy access to the "current working dir" algorithm.Junto
It seems to work like doing a sequence of cd (component) from the command line. Sounds reasonable to me.Lunn
I use this trim to get the desired effect string strFilePath = Path.Combine(basePath, otherPath.TrimStart(new char[] {'\\', '/' }) );Masterpiece
blogs.msdn.microsoft.com/jeremykuhne/2016/04/21/…Everson
I did change my working code into Path.Combine just to be safe but then it broke.. It's so stupid :)Ingle
S
30

I wanted to solve this problem:

string sample1 = "configuration/config.xml";
string sample2 = "/configuration/config.xml";
string sample3 = "\\configuration/config.xml";

string dir1 = "c:\\temp";
string dir2 = "c:\\temp\\";
string dir3 = "c:\\temp/";

string path1 = PathCombine(dir1, sample1);
string path2 = PathCombine(dir1, sample2);
string path3 = PathCombine(dir1, sample3);

string path4 = PathCombine(dir2, sample1);
string path5 = PathCombine(dir2, sample2);
string path6 = PathCombine(dir2, sample3);

string path7 = PathCombine(dir3, sample1);
string path8 = PathCombine(dir3, sample2);
string path9 = PathCombine(dir3, sample3);

Of course, all paths 1-9 should contain an equivalent string in the end. Here is the PathCombine method I came up with:

private string PathCombine(string path1, string path2)
{
    if (Path.IsPathRooted(path2))
    {
        path2 = path2.TrimStart(Path.DirectorySeparatorChar);
        path2 = path2.TrimStart(Path.AltDirectorySeparatorChar);
    }

    return Path.Combine(path1, path2);
}

I also think that it is quite annoying that this string handling has to be done manually, and I'd be interested in the reason behind this.

Socio answered 30/6, 2015 at 6:52 Comment(1)
I just used string.Concat(path1, path2) and it worked ok for my case.Schipperke
C
25

This is the disassembled code from .NET Reflector for Path.Combine method. Check IsPathRooted function. If the second path is rooted (starts with a DirectorySeparatorChar), return second path as it is.

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);
}


public static bool IsPathRooted(string path)
{
    if (path != null)
    {
        CheckInvalidPathChars(path);
        int length = path.Length;
        if (
              (
                  (length >= 1) &&
                  (
                      (path[0] == DirectorySeparatorChar) ||
                      (path[0] == AltDirectorySeparatorChar)
                  )
              )

              ||

              ((length >= 2) &&
              (path[1] == VolumeSeparatorChar))
           )
        {
            return true;
        }
    }
    return false;
}
Cricket answered 9/9, 2008 at 23:17 Comment(0)
E
24

In my opinion this is a bug. The problem is that there are two different types of "absolute" paths. The path "d:\mydir\myfile.txt" is absolute, the path "\mydir\myfile.txt" is also considered to be "absolute" even though it is missing the drive letter. The correct behavior, in my opinion, would be to prepend the drive letter from the first path when the second path starts with the directory separator (and is not a UNC path). I would recommend writing your own helper wrapper function which has the behavior you desire if you need it.

Elbring answered 10/9, 2008 at 7:12 Comment(2)
It matches the spec, but it's not what I would have expected either.Taskwork
@Jake That's not avoiding a bugfix; that's several people thinking long and hard about how to do something, and then sticking to whatever they agree on. Also, note the difference between the .Net framework (a library which contains Path.Combine) and the C# language.Oryx
I
11

Following Christian Graus' advice in his "Things I Hate about Microsoft" blog titled "Path.Combine is essentially useless.", here is my solution:

public static class Pathy
{
    public static string Combine(string path1, string path2)
    {
        if (path1 == null) return path2
        else if (path2 == null) return path1
        else return path1.Trim().TrimEnd(System.IO.Path.DirectorySeparatorChar)
           + System.IO.Path.DirectorySeparatorChar
           + path2.Trim().TrimStart(System.IO.Path.DirectorySeparatorChar);
    }

    public static string Combine(string path1, string path2, string path3)
    {
        return Combine(Combine(path1, path2), path3);
    }
}

Some advise that the namespaces should collide, ... I went with Pathy, as a slight, and to avoid namespace collision with System.IO.Path.

Edit: Added null parameter checks

Iives answered 19/4, 2017 at 23:59 Comment(0)
D
9

From MSDN:

If one of the specified paths is a zero-length string, this method returns the other path. If path2 contains an absolute path, this method returns path2.

In your example, path2 is absolute.

Ditchwater answered 9/9, 2008 at 23:13 Comment(0)
Z
9

Reason:

Your second URL is considered an absolute path, and the Combine method will only return the last path if the last path is an absolute path.

Solution:

Just remove the leading slash / from your second Path (/SecondPath to SecondPath), and it would work as excepted.

Zildjian answered 14/5, 2018 at 21:23 Comment(0)
D
7

This code should do the trick:

        string strFinalPath = string.Empty;
        string normalizedFirstPath = Path1.TrimEnd(new char[] { '\\' });
        string normalizedSecondPath = Path2.TrimStart(new char[] { '\\' });
        strFinalPath =  Path.Combine(normalizedFirstPath, normalizedSecondPath);
        return strFinalPath;
Disenfranchise answered 3/12, 2014 at 11:59 Comment(0)
O
6

Not knowing the actual details, my guess is that it makes an attempt to join like you might join relative URIs. For example:

urljoin('/some/abs/path', '../other') = '/some/abs/other'

This means that when you join a path with a preceding slash, you are actually joining one base to another, in which case the second gets precedence.

Ouphe answered 9/9, 2008 at 23:8 Comment(1)
I think the forward slashes should be explained. Also, what has this to do with .NET?Foreknow
P
3

If you want to combine both paths without losing any path you can use this:

?Path.Combine(@"C:\test", @"\test".Substring(0, 1) == @"\" ? @"\test".Substring(1, @"\test".Length - 1) : @"\test");

Or with variables:

string Path1 = @"C:\Test";
string Path2 = @"\test";
string FullPath = Path.Combine(Path1, Path2.IsRooted() ? Path2.Substring(1, Path2.Length - 1) : Path2);

Both cases return "C:\test\test".

First, I evaluate if Path2 starts with / and if it is true, return Path2 without the first character. Otherwise, return the full Path2.

Pentose answered 18/7, 2014 at 11:10 Comment(2)
Its probably safer to replace the == @"\" check by a Path.IsRooted() call since "\" isn't the only character to account for.Vanover
You can use .Trim('\') insteadRatan
E
3

This actually makes sense, in some way, considering how (relative) paths are treated usually:

string GetFullPath(string path)
{
     string baseDir = @"C:\Users\Foo.Bar";
     return Path.Combine(baseDir, path);
}

// Get full path for RELATIVE file path
GetFullPath("file.txt"); // = C:\Users\Foo.Bar\file.txt

// Get full path for ROOTED file path
GetFullPath(@"C:\Temp\file.txt"); // = C:\Temp\file.txt

The real question is: Why are paths, which start with "\", considered "rooted"? This was new to me too, but it works that way on Windows:

new FileInfo("\windows"); // FullName = C:\Windows, Exists = True
new FileInfo("windows"); // FullName = C:\Users\Foo.Bar\Windows, Exists = False
Everson answered 12/1, 2017 at 14:30 Comment(0)
Q
3

I used aggregate function to force paths combine as below:

public class MyPath    
{
    public static string ForceCombine(params string[] paths)
    {
        return paths.Aggregate((x, y) => Path.Combine(x, y.TrimStart('\\')));
    }
}
Quartus answered 20/9, 2019 at 7:17 Comment(1)
This one works as it can inserted wherever the issue lies. On a side note: what an annoying issue!Sakai
P
1

Remove the starting slash ('\') in the second parameter (path2) of Path.Combine.

Plank answered 12/9, 2019 at 21:46 Comment(1)
The question isn't asking this.Amoreta
S
0

This \ means "the root directory of the current drive". In your example it means the "test" folder in the current drive's root directory. So, this can be equal to "c:\test".

Salangia answered 23/4, 2014 at 21:51 Comment(0)
O
0

These two methods should save you from accidentally joining two strings that both have the delimiter in them.

    public static string Combine(string x, string y, char delimiter) {
        return $"{ x.TrimEnd(delimiter) }{ delimiter }{ y.TrimStart(delimiter) }";
    }

    public static string Combine(string[] xs, char delimiter) {
        if (xs.Length < 1) return string.Empty;
        if (xs.Length == 1) return xs[0];
        var x = Combine(xs[0], xs[1], delimiter);
        if (xs.Length == 2) return x;
        var ys = new List<string>();
        ys.Add(x);
        ys.AddRange(xs.Skip(2).ToList());
        return Combine(ys.ToArray(), delimiter);
    }
Oilcloth answered 1/8, 2017 at 17:12 Comment(0)
M
0

As mentiond by Ryan it's doing exactly what the documentation says.

From DOS times, current disk, and current path are distinguished. \ is the root path, but for the CURRENT DISK.

For every "disk" there is a separate "current path". If you change the disk using cd D: you do not change the current path to D:\, but to: "D:\whatever\was\the\last\path\accessed\on\this\disk"...

So, in windows, a literal @"\x" means: "CURRENTDISK:\x". Hence Path.Combine(@"C:\x", @"\y") has as second parameter a root path, not a relative, though not in a known disk... And since it is not known which might be the «current disk», python returns "\\y".

>cd C:
>cd \mydironC\apath
>cd D:
>cd \mydironD\bpath
>cd C:
>cd
>C:\mydironC\apath
Maleate answered 17/12, 2019 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.