运用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);
                

            }
        }
      
    }   
}


 

举报

相关文章推荐

返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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