利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
2015-09-17 21:21
337 查看
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹。
深搜算法用到了递归。
下面是宽搜,用到一个全局队列。
其中,用到了一个imageList组件,用它来提供一个节点的图像。
效果如图。
深搜算法用到了递归。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace 文本文件 { //public partial class Form1 : Form { // public Form1() { // InitializeComponent(); // } // private void Form1_Load(object sender, EventArgs e) { // string path = "DelegetTestSpace"; // string[] dirs = Directory.GetDirectories(path); // string[] files = Directory.GetFiles(path); // TreeNode node = new TreeNode("DelegetTestSpace"); // tvContentOfTable.Nodes.Add(node); // node.ImageIndex = 0; // node.SelectedImageIndex = 0; // DFS(path, node); // } // // 采用深度优先算法加入节点。 // private static void DFS(string path, TreeNode node) { // string[] dirs = Directory.GetDirectories(path); // string[] files = Directory.GetFiles(path); // foreach (string dir in dirs) { // TreeNode subnode = node.Nodes.Add(Path.GetFileName(dir)); // subnode.ImageIndex = 0; // subnode.ImageIndex = 0; // DFS(dir,subnode); // } // foreach (string file in files) { // TreeNode filenode = node.Nodes.Add(Path.GetFileName(file)); // filenode.ImageIndex =1; // filenode.SelectedImageIndex = 1; // // 这个tag里面包含了这个文件的完整路径。是这个节点的附加信息。 // filenode.Tag = file; // } // } // private void tvContentOfTable_AfterSelect(object sender, TreeViewEventArgs e) { // // 一般的,在EventArgs前面有内容,都是当前的信息。这里指的是点击的节点 古可以得到这个节点的附加信息。 // string tag; // // 文件夹节点没有tag // if (e.Node.Tag != null) { // tag = e.Node.Tag.ToString(); // tbContent.Text = File.ReadAllText(tag, Encoding.Default); // } // } //}
下面是宽搜,用到一个全局队列。
public partial class Form1:Form { public Form1() { InitializeComponent(); } public struct dictionary { public TreeNode node; public string str; } Queue<dictionary> queue = new Queue<dictionary>(); private void Form1_Load(object sender, EventArgs e) { string path = "DelegetTestSpace"; dictionary dic = new dictionary(); dic.node = new TreeNode(path); dic.str = path; tvContentOfTable.Nodes.Add(dic.node); BFS(dic); } private void BFS(dictionary dic) { dictionary temp = new dictionary(); queue.Enqueue(dic); while (true) { if (queue.Count==0) { break; } temp = queue.Dequeue(); TreeNode node = temp.node; string str = temp.str; string[] dirs = Directory.GetDirectories(str); string[] files = Directory.GetFiles(str); //入队文件夹 foreach (string dir in dirs) { string lastName = Path.GetFileName(dir); TreeNode subNode = new TreeNode(lastName); dictionary subDic = new dictionary(); subDic.node = subNode; subDic.str = dir; queue.Enqueue(subDic); node.Nodes.Add(subNode); } //入队文件 foreach (string file in files) { dictionary fileDic = new dictionary(); string lastName = Path.GetFileName(file); fileDic.node = new TreeNode(lastName); fileDic.node.Tag = file; node.Nodes.Add(fileDic.node); } } } private void tvContentOfTable_AfterSelect(object sender, TreeViewEventArgs e) { // 一般的,在EventArgs前面有内容,都是当前的信息。这里指的是点击的节点 古可以得到这个节点的附加信息。 string tag; // 文件夹节点没有tag if (e.Node.Tag != null) { tag = e.Node.Tag.ToString(); tbContent.Text = File.ReadAllText(tag, Encoding.Default); } }
其中,用到了一个imageList组件,用它来提供一个节点的图像。
效果如图。
相关文章推荐
- setAttibute和getParameter的区别
- Segue
- bzoj1650:跳石子
- UVA-12113 Overlapping Squares (回溯+暴力)
- 三阶魔方中心块调整配方和记忆方法
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
- 面试中常问的关于链表的题目
- Hashmap的简单实用(成果展示)
- HTML float浮动标签和background 背景标签
- javascript中的数据比较逻辑等
- weblogic初始(一)
- Java虚拟机类装载的原理及实现(转)
- ES6详解三:class
- Redis学习12之jedis的set时间测试
- 09-16 压缩图片
- isPowerOfTwo
- java中JVM虚拟机内存模型详细说明
- leetcode--Merge Intervals
- androidday01_html
- Understand Android Activity's launchMode: standard, singleTop, singleTask and singleInstance