0

With the help of this link How to display directories in a TreeView? I was able to add a directory into treeview with the method "Buildtree". Now my next step is complicated. The directory I added has several subdirectories and each one of them has files with the building date as their names.

What I shall do now is in my treeview of my program, between the level of subdirectory and the level of files I should add another level of node, with the date of files as its name (e.g. 140422). The files with this date (140422) will be folded in this node. (Since I will have hundreds of files in a folder, I shall categarize them according to their date into different upper level nodes).

I think I shall modify the "Buildtree" method but I don't know how. Any help or ideas? Thanks in advance

Community
  • 1
  • 1
jcraffael
  • 81
  • 3
  • 12
  • 1
    I suggest you 'preprocess' the directory structure, so you already have the intended structure in memory, then just display that in the Treeview. (Still looking up specifics for answer) – Flater May 23 '14 at 14:21

1 Answers1

0

Maybe something like this, if I understood the problem well:

private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe)
{
    TreeNode curNode = addInMe.Add(directoryInfo.Name);

    foreach (FileInfo file in directoryInfo.GetFiles())
    {
        string date = "getyourdatefrom_file";
        TreeNode dateNode = addInMe.Add(date);
        curNode.Nodes.Add(dateNode);
        dateNode.Nodes.Add(file.FullName, file.Name);
    }
    foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
    {
        BuildTree(subdir, curNode.Nodes);
    }
}

You basically create a node with your date as name, and then add the nodes corresponding to the files to THIS node instead of curNode.

Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • Thank you very much for the reply! That's exactly I want to do!(since I'm not allowed to attach a pic, I'm so glad that u got the idea!) However due to my incomplete description, I missed to tell that the files in each subfolder are hundreds with different dates(e.g. 140422, 140423....) and they are already sorted by dates (first 10 files of 140422, then 10 files of 140423...) – jcraffael May 23 '14 at 15:13
  • Which means I shall firstly loop througth these files to know their dates and make sure that all files of 140422 are under the node 140422, all files of 140423 are under the node 140423..etc. Seems that your solution creats 1 upper level node for each file, which isn't the way I want. So you have any idea about this? thanks again :) – jcraffael May 23 '14 at 15:14
  • Well, you could create any folders you want, but I don't think the point of the question was to search how to sort your files :p I suggest you ask a new question about that, and give the format of your files' names so people can get you a nice Regex to do all that. You could put your question like **How to get different folders' names from files' names** `I have this code [put the BuildTree code here], how can I replace "getyourdatefrom_file" by the date that is in my file's name, its format being "uselessstuff-DATE.txt"` – Kilazur May 23 '14 at 16:19
  • Well to get folder names from file names is not the point of my problem but to insert upper level nodes in the right places is key point I want to know....anyway thanks for your code, that already enlightened me! – jcraffael May 23 '14 at 16:28
  • I mean, you will have at most one new folder to create per file, right? If it's the case, this code won't change much in it structure, you will just replace the string creation. – Kilazur May 23 '14 at 17:51
  • Well I found that it is true that it is a problem to get the folder names from the files. And I really need to post a new question about that. Here's the link: http://stackoverflow.com/questions/23867903/using-foreach-to-store-names-of-one-array-in-another-in-c-sharp – jcraffael May 26 '14 at 10:18
  • I'll look into it. For now, if this answer works for the actual problem here, you can accept it. – Kilazur May 26 '14 at 10:43