絶対パスを定義したとしましょう
string abs = "X:/A/B/Q";
および相対パス
string rel = "../B/W";
これら2つを組み合わせて、次の出力が得られるようにするにはどうすればよいですか?
"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));
}
}