C#读取一个目录,然后点击其中一个节点动态加载他的子目录和文件,节省开销

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 test_Default9 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            TreeNode tp = new TreeNode("0", "0");

            Fun(@"D:/WINDOWS", tp);

            //for (int i=0; i < 10; i++)
            //{
            //    Button bnt = new Button();
            //    bnt.Text = "下载";
            //    bnt.ID = i.ToString();
            //    bnt.Click += new EventHandler(bnt_Click);
            //    TreeNode tn = new TreeNode(bnt,i.ToString());
            //    TreeView1.Nodes.Add(tn);
            //}

            //TreeView1.CollapseAll();
        }

    }

    public void Fun(string path, TreeNode tnn)
    {
        path += "//";
        TreeNode tn = new TreeNode(path, path);
        if (tnn.Value == "0")
        {
            tnn = tn;
            TreeView1.Nodes.Add(tnn);
        }


        //没有子目录
        foreach (string name in Directory.GetFiles(path))
        {
            int n = name.LastIndexOf(@"/");
            int tnw = name.Length;
            string s = name.Substring(n + 1, tnw - n - 1);
            TreeNode tn1 = new TreeNode(s, name);
            tnn.ChildNodes.Add(tn1);
        }
        foreach (string name in Directory.GetDirectories(path))
        {
            int n = name.LastIndexOf(@"/");
            int tnw = name.Length;
            string s = name.Substring(n + 1, tnw - n - 1);
            TreeNode tn2 = new TreeNode(s, name);
            tnn.ChildNodes.Add(tn2);
            // Fun(name, tn2);
        }
    }


    //    public static void fun(string path)
    //{
    //    
    //    if (Directory.GetDirectories(path).Length == 0)

    //    
    //           Response.Write(path);
    //        foreach (string name in Directory.GetFiles(path))
    //        Response.Write(name);
    //        
    //    
    //    elsehttp://163.foloda.com/topic/bj/homechild/
    //    
    //        Response.Write(path);
    //        foreach (string name in Directory.GetDirectories(path))
    //        fun(name);
    //        foreach (string name in Directory.GetFiles(path))
    //        Response.Write(name);
    //    
    //}

 


    void bnt_Click(object sender, EventArgs e)
    {
        Response.Write(((Button)sender).ID.ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        FileInfo MyFileInfo;
        //String MyFileName= Server.MapPath("Images") + "//" +
        //       this.ListBox1.SelectedItem.Text;
        String MyFileName = @"F:/完成的项目/1.27服务器/4.30/software.rar";
        MyFileInfo = new FileInfo(MyFileName);
        Response.Clear();
        Response.AddHeader("Content-Disposition",
       "attachment; filename=aaa.rar");
        //Response.AddHeader("Content-Disposition",
        //       "attachment; filename=" + this.ListBox1.SelectedItem.Text);
        Response.AddHeader("Content-Length", MyFileInfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(MyFileName);
        Response.End();
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {

        FileInfo MyFileInfo;
        //String MyFileName= Server.MapPath("Images") + "//" +
        //       this.ListBox1.SelectedItem.Text;
        String MyFileName = TreeView1.SelectedNode.Value.ToString();
        int n = TreeView1.SelectedNode.Value.ToString().LastIndexOf(@"/");
        int tn = TreeView1.SelectedNode.Value.ToString().Length;
        string s = TreeView1.SelectedNode.Value.ToString().Substring(n + 1, tn - n - 1);

        if (File.Exists(TreeView1.SelectedNode.Value.ToString()))
        {
            MyFileInfo = new FileInfo(MyFileName);
            Response.Clear();
            Response.AddHeader("Content-Disposition",
           "attachment; filename=" + s.ToString());
            //Response.AddHeader("Content-Disposition",
            //       "attachment; filename=" + this.ListBox1.SelectedItem.Text);
            Response.AddHeader("Content-Length", MyFileInfo.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(MyFileName);
            Response.End();
        }
        else
        {
            Fun(TreeView1.SelectedNode.Value.ToString(), TreeView1.SelectedNode);

        }
    }
    protected void bt1_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "s", "alert('1')", true);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "s", "alert('2')", true);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值