Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this Path.Combine work? [duplicate]

Tags:

c#

io

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:\?

like image 339
Exitos Avatar asked Aug 03 '11 15:08

Exitos


People also ask

How does PATH combine work?

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 .

Why we use path combine?

Path. Combine uses the Path. PathSeparator and it checks whether the first path has already a separator at the end so it will not duplicate the separators.


1 Answers

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.

like image 138
Jon Skeet Avatar answered Oct 01 '22 02:10

Jon Skeet