有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

短c#代替Java的路径。我的兄弟姐妹。不可能?

我有一个文件路径string filePath = @"C:\MyDir\MySubDir\myfile.ext";和另一个文件名string file2 = "otherfile.txt"。假设我想获得另一个文件路径string filePath2 = @"C:\MyDir\MySubDir\otherfile.txt";

在c#中是否有创建此类文件路径2的方法

在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".


共 (1) 个答案

  1. # 1 楼答案

    类似这样(第一个文件的目录名和第二个文件名组合在一起):

      string filePath = @"C:\MyDir\MySubDir\myfile.ext";
      string file2 = "otherfile.txt";
    
      // C:\MyDir\MySubDir\otherfile.txt
      string result = Path.Combine(Path.GetDirectoryName(filePath), file2);