<% @Import Namespace="System.IO" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnGetFileList_Click(object sender, EventArgs e)
{
string strCurDir,FileName,FileExt;
//文件大小
long FileSize;
//最后修改时间;
DateTime FileModify;
//初始化
if(!IsPostBack)
{
//初始化时,默认为当前页面所在的目录
strCurDir = Server.MapPath(".");
lblCurDir.Text = strCurDir;
txtCurDir.Text = strCurDir;
}
else
{
strCurDir = txtCurDir.Text;
txtCurDir.Text = strCurDir;
lblCurDir.Text = strCurDir;
}
FileInfo fi;
DirectoryInfo dir;
TableCell td;
TableRow tr;
tr = new TableRow();
//动态添加单元格内容
td = new TableCell();
td.Controls.Add(new LiteralControl("文件名"));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl("文件类型"));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl("文件大小"));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl("最后修改时间"));
tr.Cells.Add(td);
tableDirInfo.Rows.Add(tr);
//针对当前目录建立目录引用对象
DirectoryInfo dirInfo = new DirectoryInfo(txtCurDir.Text);
//循环判断当前目录下的文件和目录
foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
{
FileName = "";
FileExt = "";
FileSize = 0;
//如果是文件
if (fsi is FileInfo)
{
fi = (FileInfo)fsi;
//取得文件名
FileName = fi.Name;
//取得文件的扩展名
FileExt = fi.Extension;
//取得文件的大小
FileSize = fi.Length;
//取得文件的最后修改时间
FileModify = fi.LastWriteTime;
}
else//否则是目录
{
dir = (DirectoryInfo)fsi;
//取得目录名
FileName = dir.Name;
//取得目录的最后修改时间
FileModify = dir.LastWriteTime;
//设置文件的扩展名为"文件夹"
FileExt = "文件夹";
}
//动态添加表格内容
tr = new TableRow();
td = new TableCell();
td.Controls.Add(new LiteralControl(FileName));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl(FileExt));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl(FileSize.ToString() + "字节"));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl(FileModify.ToString("yyyy-mm-dd hh:mm:ss")));
tr.Cells.Add(td);
tableDirInfo.Rows.Add(tr);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>asp.net取得指定目录的文件列表</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblCurDir" style="Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 80px"
runat="server" ForeColor="Red" Width="464px">Label</asp:Label>
<asp:TextBox id="txtCurDir" style="Z-INDEX: 102; LEFT: 128px; POSITION: absolute; TOP: 40px" Text="c:\\"
runat="server"></asp:TextBox>
<asp:Table id="tableDirInfo" style="Z-INDEX: 103; LEFT: 48px; POSITION: absolute; TOP: 144px"
runat="server"></asp:Table>
<asp:Button id="btnGetFileList" style="Z-INDEX: 104; LEFT: 312px; POSITION: absolute; TOP: 40px"
runat="server" Text="取得指定目录的文件列表" OnClick="btnGetFileList_Click"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 32px; POSITION: absolute; TOP: 40px" runat="server">请指定目录</asp:Label>
<hr style="Z-INDEX: 106; LEFT: 32px; WIDTH: 54.14%; POSITION: absolute; TOP: 128px; HEIGHT: 1px"
width="54.14%" size="1">
<hr style="Z-INDEX: 107; LEFT: 40px; WIDTH: 52.51%; POSITION: absolute; TOP: 72px; HEIGHT: 1px"
width="52.51%" size="1">
<asp:Label id="Label2" style="Z-INDEX: 108; LEFT: 32px; POSITION: absolute; TOP: 80px" runat="server">当前目录为</asp:Label>
</div>
</form>
</body>
</html>