如何在C#中加入两条路径?


Answers:


158

您必须按照以下示例使用Path.Combine()

string basePath = @"c:\temp";
string filePath = "test.txt";
string combinedPath = Path.Combine(basePath, filePath); 
// produces c:\temp\test.txt

14
值得注意的是,如果“ filePath”包含绝对路径,则Path.Combine仅返回“ filePath”。 string basePath = @"c:\temp\"; string filePath = @"c:\dev\test.txt"; /* for whatever reason */ string combined = Path.Combine(basePath, filePath);产生@“ c:\ dev \ test.txt”
Jan'splite'K.

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.