ASP.NET 模拟Windows资源管理器

  Windows中文件资源管理主要是通过“Windows资源管理器”来完成,如果想在网络上实现文件的远程浏览,可以通过如下几种方法:

  1、FTP

  2、Remote Desktop Manager

  3、WWW

  其中前两种方法只能是一对一的管理,不能针对多人资源共享的情况(比如我想将服务器上的各种资源提供给外部浏览、下载),而通过WWW进行文件资源的访问则是可行的方法,使用者只要有浏览器就能访问网络资源,没有其他的限制。

  Windows资源管理器主要由三个部分组成,如图:


  三个部分的作用是:

  地址栏:显示文件或目录的地址,可以点击任何一级进入相应的位置;

  文件树:显示从根目录到所有子目录(不包括文件)的树状结构;

  目录或文件:显示打开位置的文件或目录;


  使用ASP.NET模拟的效果如下:



  在Web.Config中指定根目录:

  <appSettings>
      <add key="tooltipnum" value="60"/>
      <add key="NetDisk" value="E:\根文件夹"/>
  </appSettings>


  实现目录的遍历函数如下:


    //递归目录

 public static void ListFolds(DirectoryInfo theDir, TreeView tvwAll, TreeNode tr0, bool goNext)
    {
        DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录
        string strValue = string.Empty;
        foreach (DirectoryInfo dirinfo in subDirectories)
        {
            TreeNode tr = new TreeNode();
            tr.Text = CommonMethods.FixLenString(dirinfo.Name.ToString(), 1, 22);
            tr.ToolTip = CommonMethods.FixLenString(dirinfo.Name.ToString(), 0, 22);//超过22个字符则显示"..."
            tr.NavigateUrl = null;
            tr.ImageUrl = CommonMethods.SetIcon("folder");

            strValue = (tr.ToolTip.Length > 0) ? tr.ToolTip : tr.Text;
            tr.Value = tr0.Value + "\\" + strValue;               

            tr0.ChildNodes.Add(tr);

            if (goNext)
            {
                //递归下一个目录
                ListFolds(dirinfo, tvwAll, tr, goNext);
            }
            else
            {
                ListFolds_Temp(dirinfo, tvwAll, tr);
            }
        }
    }
  

  为提高首页的显示利率,目录树最好不要一次性加载完,打开首页时只显示第一层目录,如果某个目录还有子目录,则在其下方添加一个“等待加载子节点 ...”子节点。当用户点击左方的加号时再动态加载下层的目录。

  实现如下:

 // 若下一层有目录,则在下一层添加“等待加载子节点 ...”节点
    public static void ListFolds_Temp(DirectoryInfo theDir, TreeView tvwAll, TreeNode tr0)
    {
        DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录

        if (subDirectories.Length > 0)
        {
            TreeNode tr = new TreeNode();
            tr.Text = "等待加载子节点 ...";
            tr.NavigateUrl = null;
            tr.ImageUrl = CommonMethods.SetIcon("folder");
            tr0.ChildNodes.Add(tr);
        }
    }



    // 点击加载所有的子目录
    protected void tvwRes_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        bool first = false;

        if (e != null)
        {
            tnSelected = e.Node;
            tnSelected.ImageUrl = CommonMethods.SetIcon("folderopen");

            foreach (TreeNode node in tnSelected.ChildNodes)
            {
                if (node.Text == "等待加载子节点 ...")
                {
                    first = true;
                }
            }

            if (first)
            {
                string strPath = e.Node.ValuePath;
                strPath = strPath.Replace("\\", "\\\\");
                strPath = strPath.TrimStart('/');

                tnSelected.ChildNodes.Clear();
                DirectoryInfo thisOne = new DirectoryInfo(TopDir + strPath);               
                clsFile.ListFolds(thisOne, tvwRes, tnSelected, true);
            }
        }       
    }




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值