运用treeview显示本地硬盘目录

转载 2012年03月21日 16:17:53
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;


public partial class modal : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillTree();
        }
    }
    private void fillTree()
    {
        this.trMenu.Nodes.Clear();
        string[] str = Directory.GetLogicalDrives();//获取本地磁盘
        for (int k = 0; k < str.Length; k++)
        {
            TreeNode drivesNode = new TreeNode(str[k]);
           this.trMenu.Nodes.Add(drivesNode);//添加本地磁盘
            drivesNode.SelectAction = TreeNodeSelectAction.SelectExpand;//响应相应事件

            drivesNode.Expanded = false;//不显示节点
            DirectoryInfo thisOne = new DirectoryInfo(str[k]);
            if (str[k] == "A:\\")//判断移动盘
                drivesNode.Expanded = false;

            else if (str[k] != null)
            {
                try
                {
                    foreach (DirectoryInfo sub in thisOne.GetDirectories())//显示文件夹
                    {
                        if (sub.GetDirectories().Length > 0 || sub.GetFiles().Length > 0)
                        {
                            TreeNode subdirNode = new TreeNode(sub.Name);
                            subdirNode.Value = sub.FullName;
                            drivesNode.ChildNodes.Add(subdirNode);
                        }
                    }

                }
                catch
                {
                    continue;
                }
            }
        }

    }


    protected void trMenu_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        if (Page.IsPostBack)
        {
            e.Node.ChildNodes.Clear();

            DirectoryInfo dircld = new DirectoryInfo(e.Node.Value);

            foreach (DirectoryInfo sub in dircld.GetDirectories())
            {
                try
                {
                    if (sub.GetDirectories().Length > 0 || sub.GetFiles().Length > 0)
                    {
                        TreeNode subdirNode = new TreeNode(sub.Name);
                        subdirNode.Value = sub.FullName;
                        e.Node.ChildNodes.Add(subdirNode);

                        #region
                        DirectoryInfo dirsub = new DirectoryInfo(sub.FullName);

                        foreach (DirectoryInfo sub1 in dirsub.GetDirectories())
                        {
                            try
                            {
                                if (sub1.GetDirectories().Length > 0 || sub1.GetFiles().Length > 0)
                                {
                                    TreeNode subNode = new TreeNode(sub1.Name);
                                    subNode.Value = sub1.FullName;
                                    subdirNode.ChildNodes.Add(subNode);
                                }
                            }
                            catch
                            {
                                continue;
                            }
                        }

                        #endregion
                    }
                }
                catch
                {
                    continue;
                }
            }
        }

    }
    protected void trMenu_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeView trvw = (TreeView)sender;

        DirectoryInfo dircurrent = new DirectoryInfo(trvw.SelectedNode.Value);
        //string path = Path.GetFullPath(dircurrent.FullName.ToString());
        //FileInfo fi = new FileInfo(path);
        


        foreach (FileInfo fi in dircurrent.GetFiles())
        {
            if (fi.Extension.ToLower() == ".jpg" || fi.Extension.ToLower() == ".jpeg" || fi.Extension.ToLower() == ".bmp" || fi.Extension.ToLower() == ".gif" || fi.Extension.ToLower() == ".png")
            {
                string path = Path.GetFullPath(dircurrent.FullName.ToString());
                string aa = dircurrent.FullName.ToString() + @"\" + fi.Name.ToString();
                response.write(aa);
                

            }
        }
      
    }   
}


 

相关文章推荐

C# 系统应用之TreeView控件显示树状磁盘文件目录及加载图标

在C#系统应用毕设U盘防御软件中需要实现文件不可恢复的删除,首先需要实现类似于资源管理器的界面,通过TreeView控件显示"我的电脑"所有磁盘文件树状目录并加载相应图标.显示结果如下图所示: 一...

移动硬盘上的一个文件夹双击打开显示:文件或目录损坏且无法读取

文件或目录损坏且无法读取 我的移动硬盘上的文件夹AndroidBase,双击打开时显示:文件或目录损坏且无法读取,这时候不要慌,按照以下步骤做,就能恢复原样。 1.同时按下windows+R 键(或...

VB的TreeView展示目录树

  • 2013年04月27日 23:54
  • 2KB
  • 下载

C#读取目录加载到TreeView

http://blog.163.com/aoxiangtiankong@yeah/blog/static/8411202620092351359634/ private void For...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:运用treeview显示本地硬盘目录
举报原因:
原因补充:

(最多只允许输入30个字)