gpt4 book ai didi

java - 短 c#'s alternative to Java' s Path.ResolveSibling。不可能?

转载 作者:行者123 更新时间:2023-11-30 08:02:33 27 4
gpt4 key购买 nike

我有一个文件路径 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);

关于java - 短 c#'s alternative to Java' s Path.ResolveSibling。不可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36885285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com