winform treeview 绑定文件夹和文件
转载:http://www.cnblogs.com/zhbsh/archive/2011/05/26/2057733.html
#region treeview 绑定文件夹和文件
/// <summary>
/// 根据文件夹绑定到树
/// </summary>
/// <param name="treeview"></param>
/// <param name="FilePath"></param>
/// <returns></returns>
public bool SetTreeNoByFilePath(TreeView treeview, string FilePath,ImageList imgs)
{
treeview.Nodes.Clear();
treeview.ImageList = imgs;
try
{
foreach (DirectoryInfo direc in new DirectoryInfo(FilePath).GetDirectories())
{
TreeNode tn = new TreeNode(direc.Name);
tn.Text = direc.FullName;
SetTreeNodeIco(tn, "dir",imgs);
tn.Tag = direc.FullName;
SetSubDirectoryTreenode(direc, tn,imgs);
treeview.Nodes.Add(tn);
}
foreach (FileInfo finfo in new DirectoryInfo(FilePath).GetFiles())
{
TreeNode temptreenode = new TreeNode(finfo.Name);
temptreenode.Tag = finfo.FullName;
temptreenode.Text = finfo.Name;
SetTreeNodeIco(temptreenode, finfo.Extension, imgs);
treeview.Nodes.Add(temptreenode);
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 设置子目录的
/// </summary>
/// <param name="direc">目录路径</param>
/// <param name="tn"></param>
/// <param name="imglist"></param>
private void SetSubDirectoryTreenode(DirectoryInfo direc, TreeNode tn,ImageList imglist)
{
foreach (DirectoryInfo dir in new DirectoryInfo(direc.FullName).GetDirectories())
{
TreeNode temptn = new TreeNode(dir.Name);
temptn.Tag = dir.FullName;
SetTreeNodeIco(temptn, "dir", imglist);
temptn.Text = dir.Name;
tn.Nodes.Add(temptn);
foreach (FileInfo fileinfo in new DirectoryInfo(dir.FullName).GetFiles())
{
TreeNode temptreenode = new TreeNode(fileinfo.Name);
temptreenode.Tag = fileinfo.FullName;
temptreenode.Text = fileinfo.Name;
SetTreeNodeIco(temptreenode, fileinfo.Extension,imglist);
temptn.Nodes.Add(temptreenode);
}
SetSubDirectoryTreenode(dir, temptn, imglist);
}
}
/// <summary>
/// 为treeview设置小图标
/// </summary>
/// <param name="tn"></param>
/// <param name="strExt"></param>
/// <param name="imgs"></param>
private void SetTreeNodeIco(TreeNode tn, string strExt, ImageList imgs)
{
string ext = strExt.Replace(".", "");
if (ext.ToLower() == "dir")
{
tn.ImageIndex = imgs.Images.IndexOfKey("close");
tn.SelectedImageIndex = imgs.Images.IndexOfKey("open");
}
else if (ext.ToLower() == "doc" || ext.ToLower() == "rar" || ext.ToLower() == "txt")
{
tn.ImageIndex = imgs.Images.IndexOfKey(ext);
tn.SelectedImageIndex = imgs.Images.IndexOfKey(ext);
}
else
{
tn.ImageIndex = imgs.Images.IndexOfKey("other");
tn.SelectedImageIndex = imgs.Images.IndexOfKey("other");
}
}
#endregion
#region 只绑定文件夹
/// <summary>
/// 根据文件夹绑定到树
/// </summary>
/// <param name="treeview"></param>
/// <param name="FilePath"></param>
/// <returns></returns>
public bool SetTreeNoByFilePath(TreeView treeview, string FilePath)
{
treeview.Nodes.Clear();
try
{
foreach (DirectoryInfo direc in new DirectoryInfo(FilePath).GetDirectories())
{
TreeNode tn = new TreeNode(direc.Name);
tn.Text = direc.FullName;
tn.Tag = direc.FullName;
SetSubDirectoryTreenode(direc, tn);
treeview.Nodes.Add(tn);
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 设置子目录的
/// </summary>
/// <param name="direc">目录路径</param>
/// <param name="tn"></param>
/// <param name="imglist"></param>
private void SetSubDirectoryTreenode(DirectoryInfo direc, TreeNode tn)
{
foreach (DirectoryInfo dir in new DirectoryInfo(direc.FullName).GetDirectories())
{
TreeNode temptn = new TreeNode(dir.Name);
temptn.Tag = dir.FullName;
temptn.Text = dir.Name;
tn.Nodes.Add(temptn);
foreach (FileInfo fileinfo in new DirectoryInfo(dir.FullName).GetFiles())
{
TreeNode temptreenode = new TreeNode(fileinfo.Name);
temptreenode.Tag = fileinfo.FullName;
temptreenode.Text = fileinfo.Name;
temptn.Nodes.Add(temptreenode);
}
SetSubDirectoryTreenode(dir, temptn);
}
}
#endregion
winform treeview 绑定文件夹和文件的更多相关文章
- VBA读取文件夹下所有文件夹及文件内容,并以树形结构展示
Const TR_LEVEL_MARK = "+"Const TR_COL_INDEX = "A"Const TR_COL_LEVEL = "E&qu ...
- asp.net 检查文件夹和文件是否存在
原文 asp.net 检查文件夹和文件是否存在 允许 path 参数指定相对或绝对路径信息. 相对路径信息被解释为相对于当前工作目录. 检查该目录是否存在之前,从 path 参数的末尾移除尾随空格. ...
- 使用FileSystemWatcher监控文件夹及文件
引言 这一周主要精力集中学习一个同事开发的本地文件搜索项目上,其中客户端添加共享文件时主要是使用FileSystemWatcher 监控文件,并在各种事件发生时向服务器发送消息. 解决方法 FileS ...
- Visual Studio(VS2012) Project&(Solution) 虚拟文件夹 & 物理文件夹
今天发生个怪事:在 Solution Explorer 中,x project 内建立文件夹(folder)时,同时在磁盘目录下也创建了同名的文件夹. 1, 原本:应该只是创建一个“虚拟文件夹”用来“ ...
- TortoiseSVN文件夹及文件图标不显示解决方法
由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能正常的使用,但是就是文件夹或文件夹的左下角就是不显示图标,这个问题前一段时间就遇到了(那个时 ...
- python 实现彻底删除文件夹和文件夹下的文件
python 中有很多内置库可以帮忙用来删除文件夹和文件,当面对要删除多个非空文件夹,并且目录层次大于3层以上时,仅使用一种内置方法是无法达到彻底删除文件夹和文件的效果的,比较low的方式是多次调用直 ...
- Projects\Portal_Content\Indexer\CiFiles文件夹下文件占用磁盘空间过大问题。
C:\Program Files\Microsoft Office Servers\12.0\Data\Office Server\Applications\9765757d-15ee-432c-94 ...
- android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位
android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位 public class FileSizeUtil { public static final int SIZETYPE_B ...
- C#操作文件夹及文件的方法的使用
本文收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内容 ...
随机推荐
- 区间(bzoj 4653)
Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置.换句话说,就是使得存在一个 x ...
- Liberty中应用的contextroot
参考:http://www-01.ibm.com/support/knowledgecenter/api/content/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ ...
- HIT 1917 2—SAT
题目大意:一国有n个党派,每个党派在议会中都有2个代表, 现要组建和平委员会,要从每个党派在议会的代表中选出1人,一共n人组成和平委员会. 已知有一些代表之间存在仇恨,也就是说他们不能同时被选为和平委 ...
- MySQL 查询语句练习2
创建表 /* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50719 Sour ...
- IEEE 802.15介绍
1. 无线通信 无线通信主要是利用无线电(Radio)射频(RF)技术的通信方式,无线网络是采用无线通信技术实现的网络无线网络可为两种: 近距离无线网络和远距离无线网络 近距离无线网络主要可分为如下两 ...
- python基础===正则表达式,常用函数re.split和re.sub
sub的用法: >>> rs = r'c..t' >>> re.sub(rs,'python','scvt dsss cvrt pocdst') 'scvt dss ...
- springmvc Converter
以下,来自于Springmvc指南第二版,第93页. Spring的Converter是可以将一种类型转为另一种类型. 例如用户输入的date类型可能有多种格式. 比如:在controller中接收一 ...
- hadoop3.1伪分布式部署
1.环境准备 系统版本:CentOS7.5 主机名:node01 hadoop3.1 的下载地址: http://mirror.bit.edu.cn/apache/hadoop/common/hado ...
- MATLAB的cftool工具箱简介
下面,通过一个例子说明cftool可视化界面工具箱的用法. 例如,已知 x = [0 0.2 0.50.8 0.9 1.3 1.4 1.9 2.1 2.2 2.5 2.6 2.9 3.0]; y = ...
- C# 对话框使用大全
对话框中我们常用了以下几种:1.文件对话框(FileDialog) 它又常用到两个: 打开文件对话框(OpenFileDialog) 保存文件对话(SaveFileDialog)2.字体对话框(Fon ...