0

I have a string with directories and subdirectories that I need to format for a treeview. For that I'm trying to sort them into objects so I can know which parts are subfolders and which aren't. This is the code I have so far and it partially works but it gets a duplicate for every subfolder. All tips are helpful :D

This is my object

public class TreeViewModel
{
    public string FolderName { get; set; }
    public List<TreeViewModel> Children { get; set; }
}

And here's the rest of the code

string tmp = @ "A
B
C
A/B
A/B/C
A/B/C/D";
List <string> folders = new(tmp.Split('\n'));
List < TreeViewModel > model = new();
for (int i = 0; i < folders.Count; i++) {
  string folderName = folders[i].Replace('\n', ' ').Trim();

  List <TreeViewModel> subfolders = new();
  for (int j = 0; j < folders.Count; j++) {
    string subfolderName = folders[j].Replace('\n', ' ').Trim();
    if (DirCompare(folderName, subfolderName)) {
      subfolders.Add(new TreeViewModel {
        FolderName = subfolderName
      });
    }
  }
  model.Add(new TreeViewModel {
    FolderName = folderName,
      Children = subfolders
  });
}

foreach(var item in model) {
  Debug.WriteLine("Folder: " + item.FolderName);
  foreach(var item1 in item.Children) {
    Debug.WriteLine("Subfolder: " + item1.FolderName);

  }
}
}

bool DirCompare(string dir1, string dir2) {
  DirectoryInfo di1 = new DirectoryInfo(dir1);
  DirectoryInfo di2 = new DirectoryInfo(dir2);
  bool isParent = false;
  while (di2.Parent != null) {
    if (di2.Parent.FullName == di1.FullName) {
      isParent = true;
      break;
    } else di2 = di2.Parent;
  }
  return isParent;
}

As you can see in my output I get a duplicate for every subfolder

Folder: A
Subfolder: A/B
Subfolder: A/B/C
Subfolder: A/B/C/D
Folder: B
Folder: C
Folder: A/B
Subfolder: A/B/C
Subfolder: A/B/C/D
Folder: A/B/C
Subfolder: A/B/C/D
Folder: A/B/C/D
Baxorr
  • 298
  • 6
  • 20
  • What do you want the output to look like? Also, there are `Directory` and `DirectoryInfo` classes that make it easier to work with folders. – Rufus L Mar 25 '21 at 21:55
  • I want the output to be objects inside objects so I can take one object and it will have all the subfolders in it. – Baxorr Mar 25 '21 at 22:03
  • How would I go about using those classes in this case? I'm using them to check if the path is a parent of one another? – Baxorr Mar 25 '21 at 22:04
  • https://stackoverflow.com/questions/63069296/build-folder-files-tree-from-string-path-with-c-sharp – Baxorr Mar 26 '21 at 09:38
  • You've shown the output you're getting, but I don't know what "objects inside objects" means. Are you saying that if a `Folder` has a parent, it should only appear in the `Children` collection of the `TreeViewModel` that represents its parent, and not as a `TreeViewModel` on it's own? Can you provide an example of the expected output? – Rufus L Mar 26 '21 at 18:03
  • Marked my question as duplicate, after searching for quite a while I found a solution – Baxorr Mar 27 '21 at 06:34

0 Answers0