zoukankan      html  css  js  c++  java
  • 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

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

    效果如图。


  • 相关阅读:
    菜鸟记录:如何获取LOGINVIEW控件状态模板中的子控件
    无法安装dotnetFramework35sp1的解决方法
    MOSS2007小技巧:不用SPD轻松删除错误页面上的问题Webpart
    在动态页面里象静态页面一样控制整个网页的缓存和更新
    烦人的网页iframe去除
    经典sql注入教程
    自己写的后台静态权限验证类
    Asp.net项目从Vs2003转换到Vs2005的常见问题大全及解决方法
    C# 相当于ASP里Eval中的计算公式的方法(超简单的方法)
    1 UNIX与Linux的发展
  • 原文地址:https://www.cnblogs.com/likeFlyingFish/p/5345987.html
Copyright © 2011-2022 走看看