TreeView控件应用--访问文件或文件夹(一)

C#TreeView访问文件或文件夹,通过递归,展开所有文件夹(类似资源管理器的树形窗体)

首先,算法是用递归算法,不断的递归文件。以此来遍历整个电脑的磁盘内容,过程也很简单。这种算法的时间复杂度太大。以至于窗体打开较慢,效率不高。

复制代码
View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FolderBrowserApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //获取所有驱动盘目录
                string[] s = Directory.GetLogicalDrives();
                //通过遍历去添加所有父节点
                foreach (string m in s)
                {
                    //父节点
                    TreeNode node = new TreeNode(m);
                    //给treeview添加节点
                    this.treeView1.Nodes.Add(node);
                    //调用方法递归出磁盘的所有文件,并将父节点和路径传入
                    expendtree(m, node);
                }
            }
            catch { }
        }
        private void expendtree(string path, TreeNode tn)
        {
            try
            {
                //获取父节点目录的子目录
                string[] s1 = Directory.GetDirectories(path);
                //子节点
                TreeNode subnode = new TreeNode();
                //通过遍历给传进来的父节点添加子节点
                foreach (string j in s1)
                {
                    subnode = new TreeNode(j);
                    tn.Nodes.Add(subnode);
                    //对文件夹不断递归,得到所有文件
                    expendtree(j, subnode);
                }
            }
            catch { }
        }
    }
}
复制代码

分析以上代码,造成打开慢的原因是一开始就加载所有数据到TreeView控件中来,为了提高效率,可以等选用选择了相关的驱动器对象再来加载相关的文件夹,这样高效了很多。所以经过修改,再添加多一个ListBox把文件也显示出来。

复制代码
View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //显示各逻辑磁盘
            string[] drivers = Directory.GetLogicalDrives();
            foreach (string driver in drivers)
            {
                TreeNode node = new TreeNode(driver);
                treeView1.Nodes.Add(node);
               
                //不再预先遍历文件夹
               // expendtree(driver, node);
            }

        }

        private void expendtree(string path, TreeNode tn)
        {
            try
            {
                //遍历指定的文件夹目录
                string[] dirs = Directory.GetDirectories(path);
                foreach (string dir in dirs)
                {
                    TreeNode subnode = new TreeNode(dir);
                    tn.Nodes.Add(subnode);

                    //不再遍历子文件夹
                  //  expendtree(dir, subnode);
                }
            }
            catch { }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //选择节点的信息
            TreeNode selnode = treeView1.SelectedNode;
            string selname = treeView1.SelectedNode.Text;

            //开始遍历选定的节点,并展开它
            expendtree(selname, selnode);
            selnode.Expand();

            //加载选定节点文件夹的相关文件
            string[] files = Directory.GetFiles(selname);
            listBox1.Items.Clear();
            foreach (string file in files)
            {
                listBox1.Items.Add(file);
            }
        }
    }
}
复制代码

分析以上代码,看到文件夹显示的都是完整路径,这不是我们想要的,我们只需要显示文件夹名就可以了,所以,需要再次修改。

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //显示各逻辑磁盘
            string[] drivers = Directory.GetLogicalDrives();
            foreach (string driver in drivers)
            {
                TreeNode node = new TreeNode(driver);
                treeView1.Nodes.Add(node);
            }

        }

        private void expendtree(string path, TreeNode tn)
        {
            try
            {
                //遍历指定的文件夹目录
                string[] dirs = Directory.GetDirectories(path);
                foreach (string dir in dirs)
                { 
                    //截取文件夹名,作节点名,不需要显示完整路径
                  string  mdir = dir.Substring(dir.LastIndexOf("\\") + 1, dir.Length - dir.LastIndexOf("\\") - 1);
                    TreeNode subnode = new TreeNode(mdir);
                    tn.Nodes.Add(subnode);
                }
            }
            catch { }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //选择节点的信息
            TreeNode selnode = treeView1.SelectedNode;
          //  string selname = treeView1.SelectedNode.Text;
           
            //修改国节点的完整路径
            string selname = selnode.FullPath;

            //开始遍历选定的节点,并展开它
            expendtree(selname, selnode);
            selnode.Expand();

            //加载选定节点文件夹的相关文件
            string[] files = Directory.GetFiles(selname);
            listBox1.Items.Clear();
            foreach (string file in files)
            {
                listBox1.Items.Add(file);
            }
        }
    }
}

 

 

 再次更改,呵呵,做成图片浏览工具,添加文件到列表框时,过滤图片文件

 

 添加父节点及节点的状态图标,先给工程中添加ImageList控件,加载四个状态图标,

 

posted on   流星落  阅读(1689)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2012年8月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示