Java 的 Path.ResolveSibling 的简短 c# 替代。不可能?
Short c#'s alternative to Java's Path.ResolveSibling. Unpossible?
我有一个文件路径 string filePath = @"C:\MyDir\MySubDir\myfile.ext";
和另一个文件名 string file2 = "otherfile.txt"
。假设我想获得另一个文件路径 string filePath2 = @"C:\MyDir\MySubDir\otherfile.txt";
。
在c#中有创建这样的filePath2的方法吗?
在Java中的方法是
Path resolveSibling(Path other)
Resolves the given path against this
path's parent path. This is useful where a file name needs to be
replaced with another file name. For example, suppose that the name
separator is "/" and a path represents "dir1/dir2/foo", then invoking
this method with the Path "bar" will result in the Path
"dir1/dir2/bar".
像这样(合并第一个文件的目录名和第二个文件名) :
string filePath = @"C:\MyDir\MySubDir\myfile.ext";
string file2 = "otherfile.txt";
// C:\MyDir\MySubDir\otherfile.txt
string result = Path.Combine(Path.GetDirectoryName(filePath), file2);
我有一个文件路径 string filePath = @"C:\MyDir\MySubDir\myfile.ext";
和另一个文件名 string file2 = "otherfile.txt"
。假设我想获得另一个文件路径 string filePath2 = @"C:\MyDir\MySubDir\otherfile.txt";
。
在c#中有创建这样的filePath2的方法吗?
在Java中的方法是
Path resolveSibling(Path other)
Resolves the given path against this path's parent path. This is useful where a file name needs to be replaced with another file name. For example, suppose that the name separator is "/" and a path represents "dir1/dir2/foo", then invoking this method with the Path "bar" will result in the Path "dir1/dir2/bar".
像这样(合并第一个文件的目录名和第二个文件名) :
string filePath = @"C:\MyDir\MySubDir\myfile.ext";
string file2 = "otherfile.txt";
// C:\MyDir\MySubDir\otherfile.txt
string result = Path.Combine(Path.GetDirectoryName(filePath), file2);