Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows C#4.0
hi,
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...
Posted 8-Nov-12 2:36am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

By using
 
Directory.GetDirectoryRoot(path)
DirectoryInfo info = Directory.GetParent(path);
 
these two lines you will get all the results what you want above.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

 
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);
}
 
 
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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);
  }
}
 
?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

<pre lang="cs">var path = @"C:\Program Files\Test1\Test2\Test3\Test4";
 
var pathItems = path.Split('\\');
 
List<String>  paths = new List<String>();
int i=0;
var seed = pathItems[i];
 
while(i < pathItems.Length-1)
{
paths.Add(seed);
seed = Path.Combine(seed, pathItems[i+1]);
i++;
}
paths.Add(seed);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100