假设我已经定义了一个绝对路径
string abs = "X:/A/B/Q";
和相对路径
string rel = "../B/W";
如何将这两者结合起来以产生以下输出?
"X:/A/B/W"
我已经尝试过了Path.Combine()
,但没有成功。
试试这个:
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
我从@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));
}
}