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);
            }
        }
    }
}

 

 

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

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){   //在加载文件时,按扩展名来过滤文件,只显示jpg和bmp文件string mfile=file.Substring(file.Length-3,3);switch (mfile){ case "jpg":case "bmp":listBox1.Items.Add(file);break;default:break;}}}private void listBox1_Click(object sender, EventArgs e){//加载图片if (listBox1.SelectedIndex > 0){string file = listBox1.SelectedItem.ToString();try{pictureBox1.Image = Image.FromFile(file);}catch{ }}}}
}

 

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

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);//逻辑磁盘图标两个状态不变node.ImageIndex = 1;node.SelectedImageIndex= 1;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);//文件夹图标两个状态有变化subnode.ImageIndex = 2;subnode.SelectedImageIndex = 3;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();try{//加载选定节点文件夹的相关文件string[] files = Directory.GetFiles(selname);listBox1.Items.Clear();foreach (string file in files){//在加载文件时,按扩展名来过滤文件,只显示jpg和bmp文件string mfile = file.Substring(file.Length - 3, 3);switch (mfile){case "jpg":case "bmp":listBox1.Items.Add(file);break;default:break;}}}catch { }}private void listBox1_Click(object sender, EventArgs e){//加载图片if (listBox1.SelectedIndex > 0){string file = listBox1.SelectedItem.ToString();try{pictureBox1.Image = Image.FromFile(file);}catch{ }}}}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/302365.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

凭自己本事单的身是一种怎样的体验?你根本配不上如此优秀的我!

全世界只有3.14 % 的人关注了数据与算法之美8月12号,微博网友烂剧斗士发了这么一条微博,称“看脱口秀大会这个哥没把我给笑死”。一位从事IT(黑客)的小哥哥,是这样回忆自己的求爱经历的↓↓#她根本配不上我这么聪明的男…

gather torch_浅谈Pytorch中的torch.gather函数的含义

pytorch中的gather函数pytorch比tensorflow更加编程友好,所以准备用pytorch试着做最近要做的一些实验。立个flag开始学习pytorch,新开一个分类整理学习pytorch中的一些踩到的泥坑。今天刚开始接触,读了一下documentation,写一个一…

WPF 右下角弹窗的简单实现

软件中经常出现右下角弹窗,从下面缓缓弹出的,这次就做个简陋的实现,思路就是在窗口加载和关闭时执行动画DoubleAnimation今天懒得做界面了,只实现了功能。看看效果:下面看看代码:主窗口添加一个按钮 &#…

你周围需要这6种人(文摘)

几乎没有什么不可思议的产品是一个人就能完成的。你需要其他人来帮助你,你也需要去帮助别人。在一个好的团队中,都需要哪种类型的人进驻?来自Forbes的Jessica Hagy告诉我们,你的周围需要这6种人:1. “怂恿者”&#xf…

今年不容易,要懂得爱护自己

冬天到了,衣服逐层加厚,脖子老是皱巴巴的,坐在位置上老是觉得周身不舒服,小木提醒下大家该爱护一下自己的颈椎。这个像缩成一团的东西,是什么?打开以后,它就变成个旅行枕啦,就是我们…

实现多租户系统的一点思考

2020年突发的新冠疫情,让在线协同办公在疫情期间成为了刚需。我们也从 2020 年的 2月3 日开始在家远程办公,直到四月份。协同办公软件一下子火爆了起来,钉钉、企业微信、特别是腾讯会议等都在疫情期间表现突出,呈现出井喷式的发展…

mysql cast 四舍五入_MySQL数据库中CAST与CONVERT函数实现类型转换的讲解

MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值。两者具体的语法如下: CAST(value as type);CONVERT(value, type);就是CAST(xxx AS 类型), CONVERT(xxx,类型)。可以转换的类型是有限制的。这个类型可以是以下值其中的一个&…

都说Python库千千万,这几个你认识不?

目前,人工智能的应用日渐广泛。而作为人工智能核心的机器学习,是一门多领域的交叉学科,专门研究计算机模拟或实现人类学习行为的方法,以获取新的知识或技能,重新组织已有的知识结构使之不断改善自身的性能。简单来说&a…

Js黑客帝国效果 文字下落 制作过程和思路

效果预览: http://jsfiddle.net/dtdxrk/m8R6b/embedded/result/ Js黑客帝国效果 文字向下落制作过程和思路 1.css控制文字竖显示 2.动态添加div 用随机数当文本 3.获取分辨率 把div随机分布到屏幕里 4.随机文字的大小和透明度 5.用setInterval定时替换文本 改变div的top值 6.定…

C# 8.0 默认接口实现

C# 8.0 默认接口实现IntroC# 8.0 开始引入了默认接口实现,也就是可以在接口里写方法实现。在之前的版本中接口上是没有办法定义实现的,方法也都是 public 的,除了接口和属性之外是不能定义其他数据的,这也意味着,接口从…

50款大数据分析神器 :你还在用Excel

全世界只有3.14 % 的人关注了数据与算法之美你平时用什么大数据分析工具?D3? R? 还是Processing?PS和计算器...只有你还在用excel!工欲善其事,必先利其器!一款好的工具可以让你事半功倍。大数据…

WEB安全测试软件

为什么80%的码农都做不了架构师?>>> 五种必会软件: SubSonic CodeSmith Professional 4.1 HttpWatch Professional IE Developer Toolbar Fiddler 是一个web调试代理。它能够记录所有客户端和服务器间的http请求,允许你监视&…

python区域增长算法_区域增长算法

嘿大家好。我真的很难搞清楚这个逻辑,希望你能帮我。在我继续之前,我只想告诉你,我是业余程序员,也是一个初学者,没有任何形式的正式计算机科学培训,所以请容忍我。:D另外,我使用的是…

P6砖家:对不起,我没.NET5高并发经验,我要跑路了!

“秒杀活动”“抢红包”“微博热搜”“12306抢票”“共享单车拉新”等都是高并发的典型业务场景,那么如何解决这些业务场景背后的难点问题呢?秒杀系统中,QPS达到10万/s时,如何定位并解决业务瓶颈?明星婚恋话题不断引爆…

孩子觉得数学难?那是底子没打好!

孩子觉得数学难?那是底子没打好!(北师大学前教育博士帮你一起塑造孩子的数学思维!)要说陪娃写作业这事儿的状态和成果,用一句诗词就能概括:我本将心向明月,奈何明月照沟渠。陪得好&a…

Android 读取meta-data元素的数据

在AndroidManifest.xml中&#xff0c;<meta-data>元素可以作为子元素&#xff0c;被包含在<activity>、<application> 、<service>和<receiver>元素中&#xff0c;但 不同的父元素&#xff0c;在应用时读取的方法也不同。 1 &#xff1a;在Activ…

python科学计算环境配置_ATLAS + NumPy + SciPy + Theano 的Python科学计算环境搭建

Theano是一个Python库&#xff0c;提供了定义、优化以及评估数学表达式的库&#xff0c;尤其适合处理高维数组。使用Theano能获得和C差不多的处理速度&#xff0c;并且当利用GPU进行计算时&#xff0c;效率要优于CPU上运行的C语言程序。利用Theano能快速验证各种算法模型。但是…

朋友圈有趣的灵魂都去哪了?这几个优质公号给你答案

全世界有3.14 % 的人已经关注了数据与算法之美又到每周限量推荐公众号的时间啦关注了那么多公众号&#xff0c;百无聊奈地看文章你是否觉得时间被浪费&#xff0c;生命被辜负了&#xff1f;在号的数量上做减法&#xff0c;质量上做加法接下来给大家推荐最近一直在阅读的几个优质…

Istio 1.10 发布及官网改版

本文译自 Istio 官方文档 [1]&#xff0c;有部分修改。北京时间 5 月 19 日&#xff0c;我们很高兴地宣布 Istio 1.10 的发布&#xff01;我们要特别感谢我们的发布经理 Sam Naser[2] 和 张之晗 [3]&#xff0c;以及整个测试和发布工作组在 1.10 中的工作。这是我们 2021 年的第…

CSS各属性表

1、CSS 背景属性&#xff08;Background&#xff09; 属性描述CSSbackground在一个声明中设置所有的背景属性。1background-attachment设置背景图像是否固定或者随着页面的其余部分滚动。1background-color设置元素的背景颜色。1background-image设置元素的背景图像。1backgrou…