如何将文件夹上的文件名添加到Panel控件?

时间:2013-09-25 04:51:21

标签: c# asp.net

我想将文件夹中的所有文件显示到面板作为该文件的链接。我这样做了。但它在流阅读器行上显示错误。说“无法将列表项转换为字符串”。请帮忙

      string[] filesPath = Directory.GetFiles(Server.MapPath("~/download_results/"));
    List<ListItem> files = new List<ListItem>();
    foreach (string path in filesPath)
    {
        files.Add(new ListItem(Path.GetFileName(path)));
    }

    ArrayList allFiles = new ArrayList();
    StreamReader reader = new StreamReader(files.ToString());
    string line = reader.ReadLine();
    while (line!= null)
    {

        if (Session["f_name"] != null)
        {
            allFiles = (ArrayList)Session["f_name"];
        }
        allFiles.Add(line);
        line = reader.ReadLine();

    }
    foreach (string fileName in allFiles)
    {
        Panel1.Controls.Add(new LiteralControl("<div>"));
        HyperLink hl = new HyperLink();
        hl.Text = fileName;
        hl.ID = fileName;
        hl.Target = "_blank";
        hl.NavigateUrl = "http://www.bbb.co/download_results/'"+fileName+"'";
        Panel1.Controls.Add(hl);
        Panel1.Controls.Add(new LiteralControl("</div>"));
    }

1 个答案:

答案 0 :(得分:0)

这个怎么样:

string[] filesPath = Directory.GetFiles(Server.MapPath("~/download_results/"));

foreach (string fileName in filesPath)
        {
            string file = Path.GetFileName(fileName);
            Panel1.Controls.Add(new LiteralControl("<div>"));
            HyperLink hl = new HyperLink();
            hl.Text = file;
            hl.ID = file;
            hl.Target = "_blank";
            hl.NavigateUrl = "http://www.bbb.co/download_results/" + file;
            Panel1.Controls.Add(hl);
            Panel1.Controls.Add(new LiteralControl("</div>"));
        }

HTH!