您的位置:首页 > 其它

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

2015-09-17 21:21 495 查看
利用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组件,用它来提供一个节点的图像。

效果如图。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: