如何将文件夹上的文件名添加到面板控件
本文关键字:添加 控件 文件名 文件夹 | 更新日期: 2023-09-27 18:32:37
我想将文件夹中的所有文件作为该文件的链接显示在面板上。我这样做过。但它在流读取器行上显示错误。说"无法将列表项转换为字符串"。请帮忙
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>"));
}
这个怎么样:
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>"));
}
唰!