明智地拆分Filepath字符串文件夹

时间:2017-02-15 04:01:28

标签: c#

假设我在此路径中有ISample.cs个文件 D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc\ISample.cs

如何以下列方式获取文件路径?

D:\TEST_SOURCE\CV
D:\TEST_SOURCE\CV\SOURCECODE
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc\ISample.cs

4 个答案:

答案 0 :(得分:3)

如下:

path = @"D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc\ISample.cs"

Console.WriteLine(path);

while (path != null) {
    path = Path.GetDirectoryName(path);
    Console.WriteLine(path);
}

输出:

D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data
D:\TEST_SOURCE\CV\SOURCECODE
D:\TEST_SOURCE\CV
D:\TEST_SOURCE
D:\

Rextester Link

答案 1 :(得分:1)

你可以这样做:

Func<DirectoryInfo, IEnumerable<string>> flattenDirectory = null;
    flattenDirectory = di =>
        di == null
            ? Enumerable.Empty<string>()
            : flattenDirectory(di.Parent).Concat(new [] { di.FullName });

Func<FileInfo, IEnumerable<string>> flattenFile =
    fi => flattenDirectory(fi.Directory).Concat(new [] { fi.FullName });            

var path = @"D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc\ISample.cs";
IEnumerable<string> parts = flattenFile(new FileInfo(path));

这让我:

D:\ 
D:\TEST_SOURCE 
D:\TEST_SOURCE\CV 
D:\TEST_SOURCE\CV\SOURCECODE 
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data 
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL 
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1 
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc 
D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc\ISample.cs 

要获得问题所要求的输出,请执行parts.Skip(2)

答案 2 :(得分:0)

您可以尝试这样的事情:

List<string> filePaths = new List<string>(); // be the list to store the List
string basePath = @"D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc\ISample.cs";
filePaths.Add(basePath); 

DirectoryInfo parentInfo = Directory.GetParent(basePath);
while (parentInfo.Parent != null)
{
    filePaths.Add(parentInfo.FullName);
    parentInfo = Directory.GetParent(parentInfo.FullName);
}

Console.WriteLine(String.Join("\n",filePaths.OrderBy(x=>x.Length)));

答案 3 :(得分:0)

您可以使用String.Split分割路径。然后使用IEnumerable.TakeSplit.Join,您可以将其与所需深度合并:

Exception in thread "main" org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 0.0 failed 1 times, most recent failure: Lost task 0.0 in stage 0.0 (TID 1, localhost): java.io.FileNotFoundException: hdfs:/namenode2.aibl.net:8020/ABDF/akhilaajith/PF_knnmodel_1231480046927236/visualise1/model_points/part-00000 (No such file or directory)

当然,您也可以迭代地执行此操作:

var path = @"D:\TEST_SOURCE\CV\SOURCECODE\ARMY.Data\ProceduresALL\test1\abdc\ISample.cs";
var pathParts = path.Split('\\');    
var pathUpToCv = string.Join("\\", pathParts.Take(3)); //"D:\TEST_SOURCE\CV"