c# - Reconstruct Tree view from List<DirectoryInfo> -


i have function getdirectories() returns me list of directory info : need reconstruct tree view out of , based on parent

here function getdirectories return list:

public list<directoryinfo> getdirectories()      {         directoryinfo di = new directoryinfo("d:\\python_code");         folders.clear();         fulldirlist(di, "*");         return folders;     }  static list<directoryinfo> folders = new list<directoryinfo>(); // list hold direcotries cannot accessed  static void getfulldirlist(directoryinfo dir, string searchpattern)     {         try         {             foreach (fileinfo f in dir.getfiles(searchpattern))             {                 files.add(f);             }         }         catch         {         }          foreach (directoryinfo d in dir.getdirectories())         {             folders.add(d);             fulldirlist(d, searchpattern);         }      } 

i need constructed list , contain directoryinfo elements i.e contains parent info .

tree view of list<directoryinfo>

so far trying elements parent same , populate tree view , recursively : not able recursive function written through .

i know other easier ways same , recruitment of big picture trying draw . extremly appretiated .

assuming have winforms treeview can use key argument of find method on nodes collection build hierarchy. don't need recursion that. here add method:

    void add(directoryinfo di)     {         if (di.parent != null )         {             // find our parent node             var node = treeview1.nodes.find(di.parent.fullname,true);             if (node.length == 0)             {                 // not found, add root                 // fullname becomes key                 treeview1.nodes.add(di.fullname, di.name);             }             else             {                 // not sure going on if node.length > 1                 // anyway, add first node, our parent                 node[0].nodes.add(di.fullname, di.name);             }         }         else         {             treeview1.nodes.add(di.fullname, di.name);         }     }  

and simple iterator drive method:

        var list = getdirectories();         foreach(var di in list)         {             add(di);         } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -