转载: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 绑定文件夹和文件的更多相关文章

  1. VBA读取文件夹下所有文件夹及文件内容,并以树形结构展示

    Const TR_LEVEL_MARK = "+"Const TR_COL_INDEX = "A"Const TR_COL_LEVEL = "E&qu ...

  2. asp.net 检查文件夹和文件是否存在

    原文  asp.net 检查文件夹和文件是否存在 允许 path 参数指定相对或绝对路径信息. 相对路径信息被解释为相对于当前工作目录. 检查该目录是否存在之前,从 path 参数的末尾移除尾随空格. ...

  3. 使用FileSystemWatcher监控文件夹及文件

    引言 这一周主要精力集中学习一个同事开发的本地文件搜索项目上,其中客户端添加共享文件时主要是使用FileSystemWatcher 监控文件,并在各种事件发生时向服务器发送消息. 解决方法 FileS ...

  4. Visual Studio(VS2012) Project&(Solution) 虚拟文件夹 & 物理文件夹

    今天发生个怪事:在 Solution Explorer 中,x project 内建立文件夹(folder)时,同时在磁盘目录下也创建了同名的文件夹. 1, 原本:应该只是创建一个“虚拟文件夹”用来“ ...

  5. TortoiseSVN文件夹及文件图标不显示解决方法

              由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能正常的使用,但是就是文件夹或文件夹的左下角就是不显示图标,这个问题前一段时间就遇到了(那个时 ...

  6. python 实现彻底删除文件夹和文件夹下的文件

    python 中有很多内置库可以帮忙用来删除文件夹和文件,当面对要删除多个非空文件夹,并且目录层次大于3层以上时,仅使用一种内置方法是无法达到彻底删除文件夹和文件的效果的,比较low的方式是多次调用直 ...

  7. Projects\Portal_Content\Indexer\CiFiles文件夹下文件占用磁盘空间过大问题。

    C:\Program Files\Microsoft Office Servers\12.0\Data\Office Server\Applications\9765757d-15ee-432c-94 ...

  8. android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位

    android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位   public class FileSizeUtil { public static final int SIZETYPE_B ...

  9. C#操作文件夹及文件的方法的使用

    本文收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内容 ...

随机推荐

  1. 网络编程:connect函数

    TCP客户用connect函数来建立与TCP服务器的连接: cpp #include<sys/socket.h> int connect(int sockfd, const struct ...

  2. 【bzoj3926- [Zjoi2015]诸神眷顾的幻想乡】广义sam

    题意:给定一棵树,每个节点有一个颜色,问树上有多少种子串(定义子串为某两个点上的路径),保证叶子节点数<=20.n<=10^5 题解: 叶子节点小于等于20,考虑将每个叶子节点作为根把树给 ...

  3. 【BZOJ4080】【WF2014】Sensor Network [随机化]

    Sensor Network Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 魔法炮来到了帝都 ...

  4. 【洛谷 P1631】 序列合并 (堆)

    题目链接 直接暴力搞是\(n\)方的复杂度.\(n^2\)个数选\(n\)个最小的,容易想到堆. 我们堆里记录两个信息:到\(A\)数组哪个位置了,到\(B\)数组哪个位置了, 我直接把这两个信息存在 ...

  5. ThinkPHP 多应用多模块建立方式

    ThinkPHP3.2.2及以后版本同一应用多模块和多应用多模块的设计已经比以前的版本更加简单快捷. 注:入口文件为index.php,内容为: <?php // +-------------- ...

  6. bzoj 2039 最小割模型

    比较明显的网络流最小割模型,对于这种模型我们需要先求获利的和,然后减去代价即可. 我们对于第i个人来说, 如果选他,会耗费A[I]的代价,那么(source,i,a[i])代表选他之后的代价,如果不选 ...

  7. 硬币问题 tarjan缩点+DP 莫涛

    2013-09-15 20:04 题目描述 有这样一个游戏,桌面上摆了N枚硬币,分别标号1-N,每枚硬币有一个分数C[i]与一个后继硬币T[i].作为游戏参与者的你,可以购买一个名为mlj的小机器人, ...

  8. LCD实验学习笔记(二):head.S

    ARM加电后从0地址开始取指执行. 连接为bin文件时时,连接脚本lcd.lds指定将head.o放在开头,所以head.S就是系统起步的地方. head.S开头就是异常向量定义,0地址就是reset ...

  9. MySQL 之 foreign key

    前段回顾 create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] ); #解释: 类型:使用限制字段必须 ...

  10. Python标准库——collections模块的Counter类

    1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...