转载: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. NOIP2005过河(青蛙过河)

    题目传送门 这道题主要是因为L长度最大可以为1e9 而石子却最多只有100个 这样就浪费了很多时间空间 所以我们压缩一波路径就可以了 剩余的就是枚举每个点以及i-y到i-x的dp了 这里要说一句为什么 ...

  2. 在DirectX11下用Stencil Buffer绘制可视化Depth Complexity

    这是一道在<Introduction to 3D Game Programming with DirectX 11>上的练习题. 要求把某个像素点上的Depth Complexity(深度 ...

  3. webstorm es6 语法报错

    设置settings javascript language version后 <script>标签加个 type <script type="text/ecmascrip ...

  4. python学记笔记 2 异步IO

    在IO编程中,我们知道CPU的速度远远快于磁盘,网络IO,在一个线程中,CPU执行速度的代码非常快,然而遇到IO操作就需要阻塞 需要等待IO操作完成才能继续下一步的动作.这种情况叫做同步IO 在IO操 ...

  5. java字节流和字符流编码格式(转自姚刚)

    /** * 字节流和字符流的区别, * (1)字符流带有缓冲,必须flush或close后数据才会从缓存中写入磁盘文件. * 字节流直接写文件. * (2)字符流在创建流的时候,指定编码,字节流在文件 ...

  6. HDU-3555

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  7. 剑指offer 面试题5 : 从尾到头打印链表

    题目: 输入一个链表的头结点,从尾到头反过来打印出每个节点的值.链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; }; 思路: 通常 ...

  8. qt include_directories没用

    include_directories之后在.cpp文件中include.h文件没有路径补全说明qt没有找到该.h文件 问题解决: 在add_executable里加入那个.cpp文件就可以了 因为c ...

  9. CentOS6.9下安装 Pika 2.2.5(新增了拷贝安装版本的办法+对于PID的位置及数据库位置的理解)

    一.环境准备 yum install -y snappy-devel  protobuf-compiler  protobuf-devel bzip2-devel  zlib-devel bzip2 ...

  10. 安装mezzanine时报:storing debug log for failure【已解决】

    同时还提示: bz2 module is not found(貌似) 解决方法: 1.重新安装python wget http://bzip.org/1.0.6/bzip2-1.0.6.tar.gz ...