16

I have the following command:

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

When I look in the debugger I can see the following:

contentFolder.FullName = "E:\\"

However

reportedContentFolderPath = "\\ReportedContent\\"

Why is the Path.Combine chopping off the E:\?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Exitos
  • 29,230
  • 38
  • 123
  • 178
  • 2
    Try it without the slash on `reportedContentFolderPath` – Mrchief Aug 03 '11 at 15:44
  • 1
    CHeck out this previous answer http://stackoverflow.com/questions/53102/why-does-path-combine-not-properly-concatenate-filenames-that-start-with-path-dir – Jeff Moore Aug 03 '11 at 15:46

6 Answers6

37

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
8

It is explained in the method 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.

I recommend you read it all to understand how all the possible combinations work out: Path.Combine Method

InBetween
  • 32,319
  • 3
  • 50
  • 90
7

Path.Combine will return the second argument if it begins with a separation character (\).

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
3

I'd bet that by specifying the slash as prefix in the second string, the Combine method assumes the current drive. Try to remove the slash.

Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
2

From MSDN (emphasis mine):

public static string Combine(string path1, string path2)

[...]

Return Value

Type: System.String The combined paths. 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.

@"\ReportedContent\" is an absolute path because it begins with a backslash.

hammar
  • 138,522
  • 17
  • 304
  • 385
1

It looks like Path.Combine thinks the two slashes E:\\ refers to a UNC path, and a UNC path should not be prefixed with a drive letter. Change the contentFolder to E:\ and it should work.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234