TreeView控件应用--访问文件或文件夹(一)
C#用TreeView访问文件或文件夹,通过递归,展开所有文件夹(类似资源管理器的树形窗体)
首先,算法是用递归算法,不断的递归文件。以此来遍历整个电脑的磁盘内容,过程也很简单。这种算法的时间复杂度太大。以至于窗体打开较慢,效率不高。
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把文件也显示出来。
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 { } } } } }