问题描述
我需要分割以下提到的路径,如
"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...
推荐答案
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
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);
}
}
?
?
这篇关于如何从下至上分割路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!