如何从下至上分割路径 [英] How split the path from bottom to top

查看:79
本文介绍了如何从下至上分割路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要分割以下提到的路径,如

"C:\ Program Files \ Test1 \ Test2 \ Test3 \ Test4"

1.C:\
2.C:\ Program Files \
3.C:\ Program Files \ Test1 \
4.C:\ Program Files \ Test1 \ Test2 \
5.C:\ Program Files \ Test1 \ Test2 \ Test3 \
6.C:\ Program Files \ Test1 \ Test2 \ Test3 \ Test4

关于此的任何建议...


i need to split the path below mentioned like

"C:\Program Files\Test1\Test2\Test3\Test4"

1.C:\
2.C:\Program Files\
3.C:\Program Files\Test1\
4.C:\Program Files\Test1\Test2\
5.C:\Program Files\Test1\Test2\Test3\
6.C:\Program Files\Test1\Test2\Test3\Test4

any suggestion on this...

推荐答案

通过使用

By using

Directory.GetDirectoryRoot(path)


DirectoryInfo info = Directory.GetParent(path);



这两行将得到上面想要的所有结果.



these two lines you will get all the results what you want above.


var path = "C:\Program Files\Test1\Test2\Test3\Test4";

var pathItems = path.Split('\');

var paths = new List<string>();

for(var i = 0; i < pathItems.Length; i++)
{
    var items = pathItems.Skip(0).Take(i+1).ToArray();

    paths.Add(string.Join(@"\", items);
}



然后,以下路径包含在变量"paths"中.

1.C:\
2.C:\ Program Files \
3.C:\ Program Files \ Test1 \
4.C:\ Program Files \ Test1 \ Test2 \
5.C:\ Program Files \ Test1 \ Test2 \ Test3 \
6.C:\ Program Files \ Test1 \ Test2 \ Test3 \ Test4



The following paths are then contained in the variable "paths".

1.C:\
2.C:\Program Files\
3.C:\Program Files\Test1\
4.C:\Program Files\Test1\Test2\
5.C:\Program Files\Test1\Test2\Test3\
6.C:\Program Files\Test1\Test2\Test3\Test4


您真的是这个意思吗
Do you really mean this
public void mysplit(string path)
{
  string[] sa = path.Split(new char[]{'\\'});

  for (int i=0; i < sa.Length; i++)
  {
    string s = (i+1) + "." + sa[0];
    for (int j=1; j<=i; j++)
    {
      s += "\\" + sa[j];
    }
    Console.WriteLine(s);
  }
}



?



?


这篇关于如何从下至上分割路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆