基于Treeview控件遍历本地磁盘
一、前言
Treeview控件常用于遍历本地文件信息,通常与Datagridview与ImageList搭配。ImageList控件用于提供小图片给TreeView控件,DatagridView通常显示TreeNode节点下文件及文件夹的信息。
效果图:

二、代码
初始化窗体:
private void ManagerForm_Load(object sender, EventArgs e)
{
InitialDataGridView(dgv_Local); //初始化本地dgv InitialTreeView(); //初始化本地tree
}
初始化DataGridView:
public void InitialDataGridView(DataGridView dgv)
{
DataGridViewColumn dgv_check = new DataGridViewCheckBoxColumn();
dgv_check.HeaderText = "选择";
dgv.Columns.Add(dgv_check); DataGridViewColumn dgv_name = new DataGridViewTextBoxColumn();
dgv_name.HeaderText = "名称";
dgv.Columns.Add(dgv_name);
dgv_name.ReadOnly = true; DataGridViewColumn dgv_length = new DataGridViewTextBoxColumn();
dgv_length.HeaderText = "大小";
dgv.Columns.Add(dgv_length);
dgv_length.ReadOnly = true; DataGridViewColumn dgv_type = new DataGridViewTextBoxColumn();
dgv_type.HeaderText = "类型";
dgv.Columns.Add(dgv_type);
dgv_type.ReadOnly = true; DataGridViewColumn dgv_version = new DataGridViewTextBoxColumn();
dgv_version.HeaderText = "版本";
dgv.Columns.Add(dgv_version);
dgv_version.ReadOnly = true; dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.AllowUserToAddRows = false;
dgv.RowHeadersVisible = false;
dgv.AllowUserToResizeRows = false;
dgv.Columns[].Width = (int)((double)(dgv.Width) * 0.1);
dgv.Columns[].Width = (int)((double)(dgv.Width) * 0.45);
}
初始化本地TreeView:
public void InitialTreeView()
{
TreeNode tv_mycomputer = new TreeNode("我的电脑");
tv_mycomputer.ImageIndex = ;
tv_mycomputer.SelectedImageIndex = ;
tv_Local.Nodes.Add(tv_mycomputer); DriveInfo[] drives = DriveInfo.GetDrives();
string driveName = "";
foreach (DriveInfo drive in drives)
{
switch (drive.DriveType)
{
case DriveType.Fixed:
driveName = "本地磁盘(" + drive.Name.Substring(, ) + ")";
break;
case DriveType.Removable:
driveName = "可移动磁盘(" + drive.Name.Substring(, ) + ")";
break;
case DriveType.CDRom:
driveName = "DVD驱动器(" + drive.Name.Substring(, ) + ")";
break;
case DriveType.Network:
driveName = "网络驱动器(" + drive.Name.Substring(, ) + ")";
break;
default:
driveName = "未知(" + drive.Name + ")";
break;
}
TreeNode tr_cd = new TreeNode();
tr_cd.Text = driveName;
tr_cd.ImageIndex = ;
tr_cd.SelectedImageIndex = ;
LoadDirectory(Path.GetFullPath(drive.Name), tr_cd); //取得第一级磁盘信息
tv_mycomputer.Nodes.Add(tr_cd);
}
}
LoadDirectory方法:
public void LoadDirectory(string path, TreeNode tNode)
{
try
{
//遍历文件夹信息
string[] directorys = Directory.GetDirectories(path);
foreach (string item in directorys)
{
if ((File.GetAttributes(item) & FileAttributes.Hidden) != FileAttributes.Hidden)
{
TreeNode tn_Dir = new TreeNode(Path.GetFileName(item));
tn_Dir.ImageIndex = ;
tn_Dir.SelectedImageIndex = ;
tn_Dir.Name = item;
tNode.Nodes.Add(tn_Dir);
}
}
if (path.Contains("System Volume Information"))
{
return;
} //遍历文件信息
string[] files = Directory.GetFiles(path);
foreach (string item in files)
{
string eName = Path.GetExtension(item);
if ((File.GetAttributes(item) & FileAttributes.Hidden) != FileAttributes.Hidden)
{
TreeNode tn_file = new TreeNode(Path.GetFileNameWithoutExtension(item));
tn_file.ImageIndex = ;
tn_file.SelectedImageIndex = ;
tn_file.Name = item;
tNode.Nodes.Add(tn_file);
}
}
}
catch { }
}
AfterExpand方法:用于点击Node时快速加载,而不是在窗体加载时直接加载整个电脑的磁盘信息,因为那样太慢了
private void tv_Local_AfterExpand(object sender, TreeViewEventArgs e)
{
if (e.Node.Level >= )
{
foreach (TreeNode tnode in e.Node.Nodes)
{
tnode.Nodes.Clear();
if (!Path.HasExtension(tnode.Name))
{
LoadDirectory(tnode.Name, tnode);
}
}
}
}
NodeMouseClick:点击Node显示该节点信息
private void tv_Local_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
Thread.Sleep();
if(e.Node.FullPath=="我的电脑")
{
txt_Local.Text = "My Computer";
}
else if(e.Node.FullPath.Contains("(")&&e.Node.Level<=)
{
txt_Local.Text = e.Node.FullPath.Split('\\')[].Split('(')[].Replace(')','\\');
}
else
{
txt_Local.Text = e.Node.Name;
this.dgv_Local.Rows.Clear();
if (e.Node.Level > )
{
Loadallinfo(e.Node.Name, dgv_Local);
}
} }
Loadallinfo:用于DataGridview显示信息
public void Loadallinfo(string path,DataGridView dgv)
{
if (Directory.Exists(path))
{
try
{
string[] directorys = Directory.GetDirectories(path); //获取选中treeview节点的子目录
foreach (string item in directorys)
{
if ((File.GetAttributes(item) & FileAttributes.Hidden) != FileAttributes.Hidden)
{
int index = dgv.Rows.Add();
dgv.Rows[index].Cells[].Value = item;
dgv.Rows[index].Cells[].Value = CountSize(GetDirectoryLength(item)).ToString();
dgv.Rows[index].Cells[].Value = "文件夹";
dgv.Rows[index].Cells[].Value = "";
}
}
if (path.Contains("System Volume Information"))
{
return;
}
string[] files = Directory.GetFiles(path);
foreach (string item in files)
{
string eName = Path.GetExtension(item);
if ((File.GetAttributes(item) & FileAttributes.Hidden) != FileAttributes.Hidden)
{
int index = dgv.Rows.Add();
dgv.Rows[index].Cells[].Value = item;
System.IO.FileInfo file = new System.IO.FileInfo(item);
dgv.Rows[index].Cells[].Value = CountSize(file.Length).ToString();
dgv.Rows[index].Cells[].Value = Path.GetExtension(item);
if (Path.GetExtension(item) == ".dll")
{
FileVersionInfo ver = FileVersionInfo.GetVersionInfo(item);
dgv.Rows[index].Cells[].Value = ver.FileVersion;
}
else
{
dgv.Rows[index].Cells[].Value = "";
}
}
}
}
catch { } }
else if (File.Exists(path))
{
try
{
string item = path;
int index = dgv.Rows.Add();
dgv.Rows[index].Cells[].Value = item;
dgv.Rows[index].Cells[].Value = CountSize(item.Length).ToString();
dgv.Rows[index].Cells[].Value = Path.GetExtension(item);
if (Path.GetExtension(item) == ".dll")
{
FileVersionInfo ver = FileVersionInfo.GetVersionInfo(item);
dgv.Rows[index].Cells[].Value = ver.FileVersion;
}
else
{
dgv.Rows[index].Cells[].Value = "";
}
}
catch { }
} }
计算文件大小:
#region 文件大小换算
public static string CountSize(long Size)
{
string m_strSize = "";
long FactSize = ;
FactSize = Size;
if (FactSize < 1024.00)
m_strSize = FactSize.ToString("F2") + " B";
else if (FactSize >= 1024.00 && FactSize < )
m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
else if (FactSize >= && FactSize < )
m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
else if (FactSize >= )
m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
return m_strSize;
}
#endregion #region 文件夹大小计算
public static long GetDirectoryLength(string path)
{
if (!Directory.Exists(path))
{
return ;
}
long size = ;
DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo fi in di.GetFiles())
{
size += fi.Length;
}
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > )
{
for (int i = ; i < dis.Length; i++)
{
size += GetDirectoryLength(dis[i].FullName);
}
}
return size;
}
#endregion
基于Treeview控件遍历本地磁盘的更多相关文章
- C#TreeView控件遍历文件夹下所有子文件夹以及文件
一直对递归的理解不深刻,有时候觉得很简单,可是用起来总会出错.这里需要在TreeView控件里显示一个文件夹下的所有目录以及文件,毫无意外的需要用到递归. 一开始,想到用递归写一个生成每一个节点(Tr ...
- 用TreeView控件遍历磁盘目录
实现效果: 知识运用: ListView控件中Items集合的Add方法 TteeView控件中Nodes集合的Add方法 实现代码: private void Form1_Load(object ...
- 【Treeview】遍历本地磁盘
一.前言 Treeview控件常用于遍历本地文件信息,通常与Datagridview与ImageList搭配.ImageList控件用于提供小图片给TreeView控件,DatagridView通常显 ...
- Visual Studio 2010下ASPX页面的TreeView控件循环遍历
如果维护一个老系统就总会遇到各种问题,而这次是TreeView的循环遍历.对于Visual Studio2010上aspx页面的TreeView控件,我感受到了什么叫集微软之大智慧.与二叉树型不一样. ...
- Delphi下Treeview控件基于节点编号的访问
有时我们需要保存和重建treeview控件,本文提供一种方法,通过以树结构节点的编号访问树结构,该控件主要提供的方法如下: function GetGlobeNumCode(inNode:T ...
- Delphi下Treeview控件基于节点编号的访问1
有时我们需要保存和重建treeview控件,本文提供一种方法,通过以树结构节点的编号访问树结构,该控件主要提供的方法如下: function GetGlobeNumCode(inNode:T ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- VB TreeView控件使用详解
来源:http://www.newxing.com/Tech/Program/VisualBasic/TreeView_587.html 三小时快速掌握TreeView树状控件的使用.能不能掌握控件的 ...
- TreeView控件例子
XmL文件代码: <?xml version="1.0" encoding="utf-8" ?> <Area> <Province ...
随机推荐
- winform设置文本框宽度 根据文字数量和字体返回宽度
_LinkLabel.Width = TextRenderer.MeasureText(_LinkLabel.Text, _LinkLabel.Font).Width;
- SQL Server 数据变更时间戳(timestamp)在复制中的运用
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 方案(Solution) 方案一(Solution One) 方案二(Solution Two ...
- MySQL 正则表达式
介绍 正则表达式用来描述或者匹配符合规则的字符串.它的用法和like比较相似,但是它又比like更强大,能够实现一些很特殊的规则匹配:正则表达式需要使用REGEXP命令,匹配上返回"1&qu ...
- JavaScript面试时候的坑洼沟洄——逗号、冒号与括号
看完了javaScript数据类型和表达式与运算符相关知识后以为可以对JavaScript笔试题牛刀小试一把了,没想到有一次次的死在逗号,冒号和括号上,不得已再看看这几个符号吧. 逗号 逗号我们常见的 ...
- 敏捷是什么?PMO是什么?
敏捷组织中PMO应遵循的准则 敏捷改变了人们的工作方式,不仅仅是开发部门,而且还包括其它的部门,例如HR.财务以及PMO等.在大多数组织中,PMO是一个控制体.它指导项目团队的规范.模板以及流程.目前 ...
- AngularJS 源码分析2
上一篇地址 本文主要分析RootScopeProvider和ParseProvider RootScopeProvider简介 今天这个rootscope可是angularjs里面比较活跃的一个pro ...
- 史上最全的 Redux 源码分析
前言 用 React + Redux 已经一段时间了,记得刚开始用Redux 的时候感觉非常绕,总搞不起里面的关系,如果大家用一段时间Redux又看了它的源码话,对你的理解会有很大的帮助.看完后,在回 ...
- Atitti 大话存储读后感 attilax总结
Atitti 大话存储读后感 attilax总结 1.1. 大话存储中心思想(主要讲了磁盘文件等存储)1 1.2. 最耐久的存储,莫过于石头了,要想几千万年的存储信息,使用石头是最好的方式了1 1.3 ...
- Spring学习记录(八)---Bean的生命周期
之前说过,在调用下面时,就创建了容器和对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml&quo ...
- iOS-SDWebimage底层实现原理
其实有些框架的实现原理,并没有想象中那么难,思想也很简单,主要是更新第三方框架的作者对自己写的代码,进行了多层封装,使代码的可读性降低,也就使得框架看起来比较难.我来实现以下SDWebimage的的曾 ...