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到目标文件夹下面.指定文件夹下面的所有内容 ...
随机推荐
- hadoop之shuffle详解
Shuffle描述着数据从map task输出到reduce task输入的这段过程. 如map 端的细节图,Shuffle在reduce端的过程也能用图上标明的三点来概括.当前reduce copy ...
- Docker原理 -- namespace与CGroup
命名空间 PID(Process ID) 进程隔离 NET(Network) 管理网络隔离 IPC(InterProcess Communication) 管理跨进程通信的访问 MNT(Mount) ...
- bzoj 1096 斜率优化DP
首先比较容易的看出来是DP,w[i]为前i个工厂的最小费用,那么w[i]=min(w[j-1]+cost(j,i))+c[i],但是这样是不work的,复杂度上明显过不去,这样我们考虑优化DP. 设A ...
- 【转】使用者角度看bionic pthread_mutex和linux futex实现
使用者角度看bionic pthread_mutex和linux futex实现 本文所大篇幅引用的参考文章主要描述针对glibc和pthread实现:而本文的考察代码主要是android的bioni ...
- Linux内核态抢占机制分析【转】
转自:http://blog.csdn.net/yiyeguzhou100/article/details/53097665 目录(?)[-] 1非抢占式和可抢占式内核的区别 21 用户态抢占User ...
- easyui获取当前选中的tabs
$("#" + $("#tabs").find("iframe")[$(".tabs-header ul li").in ...
- 【POI2017||bzoj4726】Sabota?
上学期putsnan过了一次,这学期认真写了一遍…… #include<bits/stdc++.h> #define N 500010 using namespace std; ]; ,n ...
- go语言实现拷贝文件
package main import ( "fmt" "io" "os" ) func main(){ list := os.Args / ...
- 关于C++编译的程序无法在新一台电脑上运行总结
最近在调用一个SDK调试一个主板的DPIO. 可是编译好的程序在开发电脑上运行没问题,到了新主板建立的电脑系统上就出问题. 总结了下要注意一下几方面. 1:程序本身要没有问题.至少在开发电脑系统环境下 ...
- html实现点击章节自动调到开头
#转载请联系 原理是用id的值结合a链接实现锚点效果.比较简单,直接放一段代码好了. <!DOCTYPE html> <html lang="en"> &l ...