Share via


Add a trailing back slash if one doesn't exist.

Question

Tuesday, May 17, 2011 4:38 AM

Hi,

I wonder whether there is any build in function for adding back slash to the end of a path?

Thank you!

 

Cheers!

All replies (3)

Tuesday, May 17, 2011 4:54 AM âś…Answered | 1 vote

Hi eCasper,

one easy Solution is a simple check with String.EndsWith and then add the backslash.

But you should also check the Path class, e.g. if you want to combine paths, you should conside using Path.Combine.

With kind regards,

Konrad


Thursday, June 14, 2012 9:23 PM | 2 votes

You can do it in a single line of code like this:

pathWithTrailingSlash = path.TrimEnd('\') + @"\;

or perhaps more efficient but maybe harder to read:

pathWithTrailingSlah = path.EndsWith(@"\)?path:path+@"\;


Thursday, October 10, 2013 2:22 AM

is this approach ok?

pathWithTrailingSlash= Path.Combine(pathWithNoTrailingSlash, " ").TrimEnd();