zoukankan      html  css  js  c++  java
  • 使用线程加载指定目录下的所有子目录和文件名称到TreeView中

    界面设计:

    从工具箱中拖放一个TreeView(teeView1)和一个Button(button1)到WinForm窗体中

    添加引用:

    using System.Threading;
    using System.IO;

    详细代码:


            #region 目录树委托定义
            private delegate void UpdateTreeViewDelegate(TreeNode node);//声明委托类型
            private static string CurrentFolderPath;
            #endregion

            #region 目录树节点操作函数
            /// <summary>
            /// 加载节点到TreeView中
            /// </summary>
            /// <param name="node">TreeNode</param>
            private void AddToTreeView(TreeNode node)
            {
                treeView1.Nodes.Add(node);
                treeView1.Refresh();
            }
            /// <summary>
            /// 递归加载指定目录下的所有子目录和文件树
            /// </summary>
            /// <param name="reg">指定目录路径</param>
            /// <param name="nodes">起始树,将在它下面建立所有子结点</param>
            internal void LoadFolderFileList(string path, TreeNode nodes)
            {
                string[] dirs = Directory.GetDirectories(path);
                string[] files = Directory.GetFiles(path);
                for (int i = 0; i < dirs.Length; i++)
                {
                    string[] info = new string[4];
                    DirectoryInfo di = new DirectoryInfo(dirs[i]);
                    TreeNode node = new TreeNode(di.Name);
                    node.Tag = di.FullName;
                    try
                    {
                        if (di.GetDirectories().Length > 0 || di.GetFiles().Length > 0)
                        {
                            LoadFolderFileList(di.FullName, node);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    catch
                    {
                        continue;
                    }            
                    nodes.Nodes.Add(node);            
                }
                for (int i = 0; i < files.Length; i++)
                {
                   
                    FileInfo fi = new FileInfo(files[i]);
                    TreeNode node = new TreeNode(fi.Name);
                    node.Tag=fi.FullName;
                    nodes.Nodes.Add(node);

                }
            }

            #endregion

  • 相关阅读:
    14.UA池和代理池
    13.scrapy框架的日志等级和请求传参
    12.scrapy框架之递归解析和post请求
    11.scrapy框架持久化存储
    10.scrapy框架简介和基础应用
    09.移动端数据爬取
    08.Python网络爬虫之图片懒加载技术、selenium和PhantomJS
    07.验证码处理
    vi编辑器
    tar 压缩命令
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306755.html
Copyright © 2011-2022 走看看