In winform, You can traverse a folder to get all the files in the current folder and all the files in the subfolders, and add them to a Treeview control, or output the file and folder name through the console. There are a variety of methods. The following describes how to do this through recursion and queue. Recursion is actually performed during function calling, so it can be summarized as implemented through stacks and queues.
Recursive Implementation
Private void getallfile (string strpath, treenode parentnode) {// obtain all files and folders in the current path string [] dirs = directory. getdirectories (strpath); string [] files = directory. getfiles (strpath); // traverse the folder array to add each folder to foreach (string dir in dirs) {// treenode node = new treenode (dir. substring (dir. lastindexof ("\") + 1); treenode node = new treenode (path. getfilename (DIR); // store the current path in the tag attribute of the node to obtain the Node path of the current node. tag = dir; // Add the current node to the parentnode under the passed node. nodes. add (node); // recursive call, pass the current folder path and current node to the past, getallfile (Dir, node);} // traverses the current file array, create a new node and add it to the passed parent node. foreach (string file in files) {// treenode node = new treenode (file. substring (file. lastindexof ("\") + 1); treenode node = new treenode (path. getfilename (File); node. tag = file; parentnode. nodes. add (node );}}
When implemented through recursion, a directory and a root node need to be transmitted in the main call function. Generally, the information of this root node is created based on this and the directory.
Private void btnview_click (Object sender, eventargs e) {// obtain the input path string STRs = txtpath. Text in the text box; If (! String. isnullorempty (STRs) {// create a node treenode Tn = new treenode (STRs. substring (STRs. lastindexof ('\') + 1); // Add the node to the tvcontent control on the page. nodes. add (TN); getallfile (STRs, TN );}}
Queue implementation:
Treeview is also used for queue implementation. The queue is a first-in-first-out structure, and the list <string> is used in the code to simulate a queue. The generic queue provided by C # is not used, the results are the same. The main function is as follows:
Static void queuedirectory (string rootdir) {list <treenode> List = new list <treenode> (); treenode rootnode = new treenode (path. getfilename (rootdir); rootnode. tag = rootdir; List. add (rootnode); // Add the node to tvcontent in the Treeview control on the interface. nodes. add (rootnode); // console. writeline (rootdir); While (true) {If (list. count () = 0) {break;} else {// obtain the nodes in the queue from treenode parentnode = list [0]; List. removeat (0); // obtain the path string parentdir = parentnode stored in the node. tag. tostring (); // obtain all files and folders in the path string [] dirs = directory. getdirectories (parentdir); string [] files = directory. getfiles (parentdir); // traverse the folder foreach (string dir in dirs) {// create a new node and add it to the queue as treenode node = new treenode (path. getfilename (DIR); node. tag = dir; parentnode. nodes. add (node); // This is mainly a list that can be added to the interface in winform. add (node); // console. writeline ("Directory:" + DIR); // used for console output,} foreach (string file in files) {treenode node = new treenode (path. getfilename (File); node. tag = file; parentnode. nodes. add (node); // console. writeline ("file:" + file );}}}}
Similar to the recursive method, the parameter can also pass the root node or not. In the code, console. writeline () is used for console output and is not required in winform. This is also implemented using treenode, because it not only obtains the Parent and Child Nodes of the current node, but also saves the current path, however, the current file path cannot be saved in the queue. The call method is similar to that in the stack. it is OK to pass a root node. You can also use the same primary function in the stack to pass the root node.