假设我已经定义了绝对路径
string abs = "X:/A/B/Q";
和相对路径
string rel = "../B/W";
如何将这两者结合起来,以便产生以下输出?
"X:/A/B/W"
我已经尝试了Path.Combine()
,但没有成功。
答案 0 :(得分:4)
试试这个:
string abs = "X:/A/B/Q";
string rel = "../../B/W";
var path = Path.GetFullPath(Path.Combine(abs,rel));
它会给你完整的绝对路径 http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
答案 1 :(得分:0)
我从@wudzik
改进了上述代码public static string ConvertRelativePathToAbsolutePath(string basePath, string path)
{
if (System.String.IsNullOrEmpty(basePath) == true || System.String.IsNullOrEmpty(path) == true) return "";
//Gets a value indicating whether the specified path string contains a root.
//This method does not verify that the path or file name exists.
if (System.IO.Path.IsPathRooted(path) == true)
{
return path;
}
else
{
return System.IO.Path.GetFullPath(System.IO.Path.Combine(basePath, path));
}
}