📄 fileslist.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace CommonFunction
{
/// <summary>
/// fileList 的摘要说明。
/// </summary>
public class fileList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtCurDir;
protected System.Web.UI.WebControls.Table tableDirInfo;
protected System.Web.UI.WebControls.Button btnGetFileList;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label lblCurDir;
private void Page_Load(object sender, System.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);
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnGetFileList.Click += new System.EventHandler(this.btnGetFileList_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnGetFileList_Click(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -