Alvin Ashcraft
Alvin Ashcraft

Reputation: 453

Looking for something similar to Path.Combine to navigate folders

A colleague of mine is looking for a method in System.IO that will do this:

var path1 = "c:\\temp\\foo\\bar";
var path2 = "..\\..\\foo2\\file.txt";

var path3 = Path.Combine2(path1, path2);
// path3 = "c:\\temp\\foo2\\file.txt"

Is there anything in System.IO to do this type of combining, or will he have to write his own method? I couldn't find anything.

Thanks!

Upvotes: 3

Views: 530

Answers (2)

Joachim Isaksson
Joachim Isaksson

Reputation: 181067

This should do it;

var path3 =  Path.GetFullPath(Path.Combine(path1, path2));

Upvotes: 4

ken2k
ken2k

Reputation: 49013

var path1 = "c:\\temp\\foo\\bar";
var path2 = "..\\..\\foo2\\file.txt";

var path3 = Path.GetFullPath(Path.Combine(path1, path2)).Normalize();

Upvotes: 9

Related Questions