lysergic-acid
lysergic-acid

Reputation: 20050

Escape a path with ".." (parent) in C#

I have a string that denotes a full path (note this is on OSX, but i believe it should have a similar solution for any OS):

var path = "/Some/Path/../Projects/iOS/ThirdParty/myPath";

Checking this path to see if it exists returns false:

Directory.Exists(path) // return false

Is there any built-in helper method or class that can help translating this into a full path that does not contain ".." ?

Upvotes: 0

Views: 218

Answers (2)

in the statement var path = "/Some/Path/../Projects/iOS/ThirdParty/myPath";

'/' acts as a escape character, you need to declare the statement by appending @ symbol at the beginning like below.

var path = @"/Some/Path/../Projects/iOS/ThirdParty/myPath";

Upvotes: -3

DavidG
DavidG

Reputation: 119096

You are attempting to convert a relative path to an absolute one. You can use Path.GetFullPath to do this:

var relativePath = "/Some/Path/../Projects/iOS/ThirdParty/myPath";
var absolutePath = Path.GetFullPath(relativePath);

Upvotes: 4

Related Questions