How do I extract each folder name from a path if I don't know how many folders there are in the path and I don't know the folder names?
-
5Writing some code in the first place would help.aquaraga– aquaraga2013-07-15 08:29:51 +00:00Commented Jul 15, 2013 at 8:29
-
stackoverflow.com/questions/3736462/…Zaki– Zaki2013-07-15 08:29:56 +00:00Commented Jul 15, 2013 at 8:29
-
stackoverflow.com/questions/2407986/…rags– rags2013-07-15 08:30:44 +00:00Commented Jul 15, 2013 at 8:30
-
Do you want it to be recursivem or just in that path - As in, do you want subfolders too?Peter Rasmussen– Peter Rasmussen2013-07-15 08:34:00 +00:00Commented Jul 15, 2013 at 8:34
Add a comment
|
3 Answers
Split the string by using seprator:
var dirs[] = completePath.Split(Path.DirectorySeparatorChar);
after iterate over each subfolder and construct possible subpaths
var composition = string.Empty;
var directoryPathList = new List<string>();
foreach(var s in dirs) {
composition += Path.DirectorySeparatorChar + s;
directoryPathList.Add(composition);
}
1 Comment
rene
+1 for using DirectorySeparatorChar
You can just use String.Split:
string fileName = @"C:\foo\bar\baz.txt";
string directory = Path.GetDirectoryName(fileName); // "C:\foo\bar"
string allDirectoryNames = directory.Split('\\'); // ["C:", "foo", "bar"]
1 Comment
Nolonar
I'd use
System.IO.Path.DirectorySeparatorChar instead of '\\'.Do you mean something like this:
String path = @"\\MyNetwork\Test\my progs\MySource.cpp";
String[] names = Path.GetDirectoryName(path).Split(new Char[] {
Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}, StringSplitOptions.RemoveEmptyEntries);
// names contains ["MyNetwork", "Test", "my progs"]