文件递归-XML递归-树图递归

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

常见的三种递归介绍:FileSystem、XML、TreeView··············································2一、文件系统递归·································································································2★详细代码如下:······························································································································2二、树图递归—数据表多层关系···············································································5★详细代码如下:······························································································································6三、XML文件递归·······························································································9★详细代码如下:··························································································································111常见的三种递归介绍:FileSystem、XML、TreeView面试中经常遇到的三种递归:文件系统递归、XML递归、树图TreeView递归。以下代码开发语言:C#,属于Windows简单应用程序。希望对初进入职场的求职者有所帮助。一、文件系统递归在VisualStudio2005中新建项目Windows应用程序:命名为RecursionDemo//在默认的Form1窗体加入三个控件Button:btnSearch,TreeView:tvFiles,ImageList:imageList1//设置tvFiles.ImageList=imageList1;//为imageList1添加3个图片★详细代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespaceRecursionDemo{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}///summary///查询按钮的Click事件绑定TreeView////summary///paramname=sender/param///paramname=e/paramprivatevoidbtnSearch_Click(objectsender,EventArgse){tvFiles.Nodes.Clear();//清空节点//用户选择文件夹获得选择的文件夹的路径FolderBrowserDialogfbd=newFolderBrowserDialog();if(fbd.ShowDialog()==DialogResult.OK){TreeNoderootNode=newTreeNode();rootNode.Text=Path.GetFileName(fbd.SelectedPath);rootNode.ImageIndex=0;rootNode.SelectedImageIndex=1;tvFiles.Nodes.Add(rootNode);//SearchFiles(rootNode.Nodes,fbd.SelectedPath);SearchFilesMutation(rootNode,fbd.SelectedPath);//用上面一行代//码也可以tvFiles.SelectedNode=rootNode;}else{MessageBox.Show(没有选择文件夹或目录!,提示);}}///summary///查询文件和所有文件夹(目录)///思路:.获得目录path下的所有子文件夹(子目录)、获得目录path下的所有文//件///2.如果是子目录,添加该子目录路径到TreeNodeCollection,继续递归子目录;///3.如果是文件,则只需添加文件路径到TreeNodeCollection////summary///paramname=nodes树节点集合/param///paramname=path当前文件夹路径/paramprivatevoidSearchFiles(TreeNodeCollectionnodes,stringpath){string[]folders=Directory.GetDirectories(path);//获取path文件夹下//所有的子文件夹foreach(stringfolderPathinfolders){TreeNodetempNode=newTreeNode();tempNode.Text=Path.GetFileName(folderPath);intcurrentIndex=nodes.Add(tempNode);nodes[currentIndex].ImageIndex=0;//树节点未选中时的图//片nodes[currentIndex].SelectedImageIndex=1;//树节点处于选中时的//图片//如果tempNode存在子目录继续遍历SearchFiles(tempNode.Nodes,folderPath);//这里递归}string[]files=Directory.GetFiles(path);//获取path文件夹下所有的文//件foreach(stringfilePathinfiles){TreeNodenode=nodes.Add(Path.GetFileName(filePath));node.ImageIndex=2;node.SelectedImageIndex=2;}}///summary///查询文件和文件夹的另一种方法,和SearchFiles方法原理一致////summary///paramname=node当前树节点/param///paramname=path当前树节点(目录)的路径/paramprivatevoidSearchFilesMutation(TreeNodenode,stringpath){//获取path下的所有文件和目录组成一个string数组数组元素为物理路径string[]fileAndFolders=Directory.GetFileSystemEntries(path);foreach(stringfileOrFolderPathinfileAndFolders){if(Directory.Exists(fileOrFolderPath))//如果是目录(文件夹){TreeNodetempNode=newTreeNode();tempNode.Text=Path.GetFileName(fileOrFolderPath);intcurrentIndex=node.Nodes.Add(tempNode);node.Nodes[currentIndex].ImageIndex=0;//树节点未选//中时的图片node.Nodes[currentIndex].SelectedImageIndex=1;//树节点处于//选中时的图片//如果tempNode存在子目录继续遍历SearchFilesMutation(tempNode,fileOrFolderPath);//这里递归}else//如果是文件即:File.Exists(fileOrFolderPath){TreeNodecurrentNode=node.Nodes.Add(Path.GetFileName(fileOrFolderPath));currentNode.ImageIndex=2;currentNode.SelectedImageIndex=2;}}}}}运行程序。点击按钮,选择一个文件夹,效果如下图(文件夹选中和未选中时的图标不同):二、树图递归—数据表多层关系以Sqlserver数据库的一张部门表Department为例,查询显示所有的部门,并按从属关系绑定到树图上。在SqlServer中,新建部门表Department,sql语句如下:CREATETABLE[Department]([DepartmentNo][varchar](12)COLLATEChinese_PRC_CI_ASNOTNULL,[ParentNo][varchar](12)COLLATEChinese_PRC_CI_ASNULL,[DepartmentName][nvarchar](30)COLLATEChinese_PRC_CI_ASNOTNULL,[Remark][nvarchar](200)COLLATEChinese_PRC_CI_ASNULL,CONSTRAINT[PK_Department]PRIMARYKEYCLUSTERED([DepartmentNo])ON[PRIMARY])ON[PRIMARY]---当ParentNo为null时,即没有父部门(上一级部门),我们认为是顶级部门。添加数据到Department。如下:在项目RecursionDemo下,添加Windows窗体Form2,在Form2上添加两个控件Button:btnExamineDepartment,TreeView:tvDepartment★详细代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceRecursionDemo{publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}///summary///查看所有部门并绑定到树图上////summary///paramname=sender/param///paramname=e/paramprivatevoidbtnExamineDepartment_Click(objectsender,EventArgse){tvDepartment.Nodes.Clear();try{DataTabledtTopDepartment=GetTopDepartemnt();//获得最顶级部门//调用递归方法BindTreeView(tvDepartment.Nodes,dtTopDepartment);}catch(Exceptionex){MessageBox.Show(ex.Message,异常信息);}}///summary///绑定

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功