对文件夹进行操作-浏览器中的文件列表
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCurrentDir;
strCurrentDir = Server.MapPath(".");
lblCurrentDir.Text = strCurrentDir;
FileInfo fi;
DirectoryInfo di;
TableCell td;
TableRow tr;
string FileName;
string FileExt;
long FileSize;
DateTime FileModify;
DirectoryInfo dir = new DirectoryInfo(strCurrentDir);
foreach (FileSystemInfo fsi in dir.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
{
di = (DirectoryInfo)fsi;
FileName = di.Name;
FileModify = di.LastWriteTime;
}
tr = new TableRow();
td = new TableCell();
td.Controls.Add(new LiteralControl(FileName.ToString()));
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()));
tr.Cells.Add(td);
tbDirInfo.Rows.Add(tr);
}
}
}
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCurrentDir;
strCurrentDir = Server.MapPath(".");
lblCurrentDir.Text = strCurrentDir;
FileInfo fi;
DirectoryInfo di;
TableCell td;
TableRow tr;
string FileName;
string FileExt;
long FileSize;
DateTime FileModify;
DirectoryInfo dir = new DirectoryInfo(strCurrentDir);
foreach (FileSystemInfo fsi in dir.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
{
di = (DirectoryInfo)fsi;
FileName = di.Name;
FileModify = di.LastWriteTime;
}
tr = new TableRow();
td = new TableCell();
td.Controls.Add(new LiteralControl(FileName.ToString()));
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()));
tr.Cells.Add(td);
tbDirInfo.Rows.Add(tr);
}
}
}
<form id="form1" runat="server">
<div>
当前目录为:<asp:Label ID="lblCurrentDir" runat="server"></asp:Label><br />
目录下文件的列表为:<br />
<asp:Table ID="tbDirInfo" runat="server">
</asp:Table>
</div>
</form>
<div>
当前目录为:<asp:Label ID="lblCurrentDir" runat="server"></asp:Label><br />
目录下文件的列表为:<br />
<asp:Table ID="tbDirInfo" runat="server">
</asp:Table>
</div>
</form>