這裏是我用它來掃描目錄的內容,並建立一個鏈接,在服務器上的文件夾裏面的文件了一些C#代碼。這聽起來像你只有一個文件,但它仍然可以正常工作,如果需要你可以調整它。
顯然這是爲了建立一個文件夾內的文件的鏈接列表,它對你的例子來說肯定有點矯枉過正,但也許它會給你一些想法。
DirectoryInfo di = default(DirectoryInfo);
FileInfo[] files = null;
DataTable dt = new DataTable();
DataRow dr = null;
System.DateTime filedate = default(System.DateTime);
di = new DirectoryInfo(Server.MapPath("~/forms"));
files = di.GetFiles();
dt.Columns.Add("name");
dt.Columns.Add("filepath");
dt.Columns.Add("filedate");
foreach (FileInfo inf in files)
{
filedate = inf.LastWriteTime;
dr = dt.NewRow();
dr["name"] = inf.Name;
dr["filepath"] = inf.FullName;
dr["filedate"] = String.Format("{0:MM/dd/yyyy}", filedate);
dt.Rows.Add(dr);
}
DataList1.DataSource = dt;
DataList1.DataBind();
而且你的aspx頁面上:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="1" GridLines="none">
<HeaderTemplate>
<table>
<tr>
<td style="width: 450px">
<asp:Label ID="label1" runat="server" Text="Form Name" Font-Bold="true"></asp:Label>
</td>
<td>
<asp:Label ID="label2" runat="server" Text="Creation Date" Font-Bold="true"></asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td style="width: 446px">
<a target="_blank" href='http://yourwebserverpath.com/forms/<%# DataBinder.Eval(Container.DataItem, "name") %>'>
<%# DataBinder.Eval(Container.DataItem, "name") %></a>
</td>
<td style="padding: 0 0 0 5px">
<%#DataBinder.Eval(Container.DataItem, "filedate")%>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<table>
<tr>
<td style="width: 446px">
</td>
<td style="padding: 0 0 0 5px">
</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
感謝弗雷德裏克,原來它是一個安全違規問題與Firefox。它在Internet Explorer中運行良好。 – kingrichard2005 2010-12-21 18:23:11