Why Does Path.Combine Not Properly Concatenate Filenames That Start with Path.Directoryseparatorchar

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

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().

Why won't this Path.Combine work?

You have a leading slash on @"\ReportedContent\". You don't want that (or the trailing one, I suspect) - try just:

string reportedContentFolderPath =
Path.Combine(contentFolder.FullName.ToString(), "ReportedContent");

From the documentation:

If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned.

In your case, path2 did contain a root, so it was returned without looking at path1.

Why Path.Combine doesn't add the Path.DirectorySeparatorChar after the drive designator?

C:filename is a valid path and is different from C:\filename. C:filename is the file filename in the current directory on the C: drive whereas C:\filename is the file filename in the root of that drive. Apparently they wanted to keep the functionality of refering to the current directory on some drive.

This behaviour is described here in MSDN

Path.Combine concatenates only last two string

From MSDN: Path.Combine(String, String, String, String)

path1 should be an absolute path (for example, "d:\archives" or
"\archives\public").If one of the subsequent paths is also an
absolute path, the combine operation discards all previously combined
paths and resets to that absolute path
.

Asper the MSDN documentation Path1 should be an Absolute Path and you should not have Absolute Path in any subsequent paths

Replace This:

string filePath = "\\Avatar";

With This:

string filePath = "Avatar";

Why does Path.Combine ignore the first part of a ftp server path?

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

c# Path.Combine isn't working right?

You shouldn't have the \ at the start of the second argument. You want:

string filepath = Path.Combine(arg1, "tf1.dat");

Otherwise it thinks you want an absolute filename, basically.

Path.Combine() behaviour with drive letters

Path.Combine works that way because c:file.txt is actually a valid path.

According to Microsoft documentation on NTFS paths:

If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter. Note that the current directory may or may not be the root directory depending on what it was set to during the most recent "change directory" operation on that disk.

In a nutshell, c:file.txt will search the file in the current directory of the C: drive, while c:\file.txt will search the file at the root folder of the drive (ignoring the current directory).

Since Path.Combine has no way to know what was the behavior you expected, it cannot automatically add backslashes.



Related Topics



Leave a reply



Submit