My situation is that I am given a List which represents a directory structure, in the following format:
"My Folder\Images" "My Folder\Images\Gif" "My Folder\Images\JPG" "My Folder\Media" "My Folder\Media\Mov" "My Folder\Media\Mov\QT" "My Folder\Media\MPG" There is no restriction on how many levels this can be nested.
I need to build something which represents a treeview from this, in the format:
public class Folder { public string FolderName { get; set; } public List<Folder> Folders{ get; set; } // a list of subfolders } I just can't get the recrusive function which builds this quite right. Any help from the Gurus would be greatly appreciated.
TIA
Edit: My full class definition is:
public class Folder { public string Name { get; set; } public List<Folder> Folders { get; set; } public Folder(List<string> input) { foreach (var folder in input) { var delimPos = folder.IndexOf("\\"); if (delimPos == -1) { Name = folder ; } else { Name = folder.Substring(0, delimPos); var subFolders= input.Select(o => o.Substring(delimPos + 1)).ToList(); Folders= new List<Folder>(); foreach (var subFolder in subFolders) { Folders.Add(new Folder(new List<string>() { subFolder })); } } } } }