利用TreeView控件加载文件,必须遍历处所有的文件和文件夹。

深搜算法用到了递归。

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组件,用它来提供一个节点的图像。

效果如图。

利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。的更多相关文章

  1. 最小生成树算法 prim kruskal两种算法实现 HDU-1863 畅通工程

    最小生成树 通俗解释:一个连通图,可将这个连通图删减任意条边,仍然保持连通图的状态并且所有边权值加起来的总和使其达到最小.这就是最小生成树 可以参考下图,便于理解 原来的图: 最小生成树(蓝色线): ...

  2. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

  3. 中间自适应,左右定宽的两种经典布局 ---- 圣杯布局 VS 双飞翼布局

    一.引子 最近学了些js框架,小有充实感,又深知如此节奏的前提需得基础扎实,于是回头想将原生CSS和Javascript回顾总结一番,先从CSS起,能集中它的就在基础的布局上,便查阅了相关资料,将布局 ...

  4. php两种include加载文件方式效率比较如下

    1)定义一个字符串变量,里面保存要加载的文件列表.然后foreach加载. $a = '/a.class.php;/Util/b.class.php;/Util/c.class.php'; $b = ...

  5. mstsc远程报:这可能是由于CredSSP 加密Oracle修正的两种完美解决方法

    win10很完美,用的也很舒服!当然人无完人,也总有不尽如人意的时候.比如说我们经常用的远程mstsc,就出现了一个坑,既然出现坑了,我们就得把坑解决掉吧!下面就记录一下这个坑的解决方法. 本文地址: ...

  6. 前台获取json未定义问题之两种常用解决办法

    来自博客园的一位朋友解答: 为什么要 eval这里要添加 “("("+data+")");//”呢? 原因在于:eval本身的问题. 由于json是以”{}”的 ...

  7. HDU 1160 排序或者通过最短路两种方法解决

    题目大意: 给定一堆点,具有x,y两个值 找到一组最多的序列,保证点由前到后,x严格上升,y严格下降,并把最大的数目和这一组根据点的编号输出来 这里用两种方法来求解: 1. 我们可以一开始就将数组根据 ...

  8. 两种方法解决 "The License CNEKJPQZEX- has been cancelled..." 问题

    今天在使用 2017 的 IDEA 和 Pycharm 等IDE的时候,提示了如题的问题.之前实在 http://idea.lanyus.com/ 网站点击生成注册码,复制粘贴到 IDEA 中就好了, ...

  9. 「每日五分钟,玩转JVM」:两种算法

    前言 上篇文章,我们了解了GC 的相关概念,这篇文章我们通过两个算法来了解如何去确定堆中的对象实例哪些是我们需要去回收的垃圾对象. 引用计数算法 引用计数法的原理很简单,就是在对象中维护一个计数器,当 ...

随机推荐

  1. 对于git的认识

    对于git的认识,我只想说,我不会把他的概念复制下来在博客上再发一遍,我对于他的了解是代源码的开放编写.对于git我会在今后去认真的理解他,不是所谓的抄袭.不会的东西我会尽力去学习.

  2. 安装 SSL 证书

    http://www.itrus.cn/html/fuwuyuzhichi/fuwuqizhengshuanzhuangpeizhizhinan/

  3. eclipse左边导航package explorer自动定位

    eclipse或myeclipse中右边编辑界面点击 左边导航自动定位     左导航Package Explorer的右上角有一个黄色双向箭头图标,鼠标移动到上面提示"Link with ...

  4. Linux内核--网络栈实现分析(八)--应用层发送数据(下)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7547826 更多请查看专栏,地 ...

  5. 修改mysql某一键为自增键

    alter table tb_name modify id int auto_increment primary key

  6. 用javascript写Android和iOS naitve应用,实在炫酷。

    关注NativeScript有一段时间了,说好了的三月发第一个Beta版,终于发布了. // declare the extended NativeScriptActivity functionali ...

  7. FFmpeg 2.1 发布

    FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它包括了目前领先的音/视频编码库 libavcodec. FFmpeg是在Linux下开发出来的,但它可以在包括W ...

  8. centos git 服务器搭建

    1.安装git sudo yum -y install git 2.创建用户与授权 # 找到 git_shell whereis git_shell /usr/bin/git-shell # 创建 g ...

  9. Ubuntu iptables配置

    sudo su sudo apt-get install iptables-persistent modprobe ip_tables #启动iptable   #删除原有iptables规则 ipt ...

  10. Request中的各种方法

    前言 Request中方法众多,对于Java Web程序员来说,种种方法都会在工作中常常用到.Request由于不是JDK的一部分,这些方法的用法也没有专门的API可以查,所以在工作中遇到Reques ...