1

絶対パスを定義したとしましょう

string abs = "X:/A/B/Q";

および相対パス

string rel = "../B/W";

これら2つを組み合わせて、次の出力が得られるようにするにはどうすればよいですか?

"X:/A/B/W"

すでに試しPath.Combine()ましたが、うまくいきませんでした。

4

2 に答える 2

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

于 2013-08-26T12:18:44.093 に答える
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));
    }
}
于 2016-05-10T04:55:53.417 に答える