简体   繁体   中英

Path.Combine two relative path strings to create new relative path

I'm trying to join two relative paths to create another relative (NOT absolute) path using Path.Combine.

string path1=@"rootDir\DirA\DirAA";
string path2=@"..\..\DirB";
Console.WriteLine(Path.Combine(path1, path2));
//I get: rootDir\DirA\DirAA\..\..\DirB
Console.WriteLine(Path.GetFullPath(Path.Combine(path1, path2)));
//I get: C:User\rootDir\DirB

What I actually want is

//rootDir\DirB

Is there any way to accomplish that using Path?

Try this:

Path.GetFullPath(Path.Combine(path1, path2))
    .Substring(Directory.GetCurrentDirectory().Length + 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM