ASP.NET 模拟Windows资源管理器

来源:互联网 发布:大掌门杨过成长数据 编辑:程序博客网 时间:2024/06/08 02:12

  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 0
原创粉丝点击