Why does Path.Combine produce this result with a relative path?

后端 未结 2 1001
余生分开走
余生分开走 2020-12-06 04:02

To my surprise, this code does not produce expected results:

var basePath = @\"\\\\server\\BaseFolder\";
var relativePath = @\"\\My\\Relative\\Folder\";

var         


        
2条回答
  •  天涯浪人
    2020-12-06 04:47

    Paths that start with a slash are interpreted as being absolute rather than relative. Simply trim the slash off if you want to guarantee that relativePath will be treated as relative.

    var basePath = @"\\server\BaseFolder";
    var relativePath = @"\My\Relative\Folder";
    
    var combinedPath = Path.Combine(basePath, relativePath.TrimStart('/', '\\'));
    

提交回复
热议问题