在此实例中仍需要先将System.IO空间引入,然后在VS编辑器中输入以下代码,且以ShowFile.aspx保存。
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 PracticeProject.Practice1
... {
/**//// <summary>
/// ShowFile 的摘要说明。
/// </summary>
public class ShowFile : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.Label lblFileFullName;
protected System.Web.UI.WebControls.Label lblFileDirectoryName;
protected System.Web.UI.WebControls.Label lblFileCreationTime;
protected System.Web.UI.WebControls.Label lblFileSize;
protected System.Web.UI.WebControls.Label lblFileLastAccessTime;
protected System.Web.UI.WebControls.Label lblFileLastWriteTime;
protected System.Web.UI.HtmlControls.HtmlGenericControl divFileContent;
protected System.Web.UI.WebControls.Label lblFileName;
private void Page_Load(object sender, System.EventArgs e)
...{
if (!IsPostBack)
...{
BindFileInfo();
}
}
private void BindFileInfo()
...{
string strFile2Show = Request.QueryString.Get("file");
FileInfo file = new FileInfo(strFile2Show);
lblFileName.Text = file.Name;
lblFileFullName.Text = file.FullName;
lblFileDirectoryName.Text = file.DirectoryName;
lblFileCreationTime.Text = file.CreationTime.ToString();
lblFileSize.Text = file.Length.ToString();
lblFileLastAccessTime.Text = file.LastAccessTime.ToString();
lblFileLastWriteTime.Text = file.LastWriteTime.ToString();
StreamReader sr = file.OpenText();
char[] theBuffer = new char[255];
int nRead = sr.ReadBlock(theBuffer,0,255);
divFileContent.InnerHtml = "<pre>";
divFileContent.InnerHtml += Server.HtmlEncode(new String(theBuffer,0,nRead));
divFileContent.InnerHtml += "</pre>";
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
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 PracticeProject.Practice1
... {
/**//// <summary>
/// ShowFile 的摘要说明。
/// </summary>
public class ShowFile : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.Label lblFileFullName;
protected System.Web.UI.WebControls.Label lblFileDirectoryName;
protected System.Web.UI.WebControls.Label lblFileCreationTime;
protected System.Web.UI.WebControls.Label lblFileSize;
protected System.Web.UI.WebControls.Label lblFileLastAccessTime;
protected System.Web.UI.WebControls.Label lblFileLastWriteTime;
protected System.Web.UI.HtmlControls.HtmlGenericControl divFileContent;
protected System.Web.UI.WebControls.Label lblFileName;
private void Page_Load(object sender, System.EventArgs e)
...{
if (!IsPostBack)
...{
BindFileInfo();
}
}
private void BindFileInfo()
...{
string strFile2Show = Request.QueryString.Get("file");
FileInfo file = new FileInfo(strFile2Show);
lblFileName.Text = file.Name;
lblFileFullName.Text = file.FullName;
lblFileDirectoryName.Text = file.DirectoryName;
lblFileCreationTime.Text = file.CreationTime.ToString();
lblFileSize.Text = file.Length.ToString();
lblFileLastAccessTime.Text = file.LastAccessTime.ToString();
lblFileLastWriteTime.Text = file.LastWriteTime.ToString();
StreamReader sr = file.OpenText();
char[] theBuffer = new char[255];
int nRead = sr.ReadBlock(theBuffer,0,255);
divFileContent.InnerHtml = "<pre>";
divFileContent.InnerHtml += Server.HtmlEncode(new String(theBuffer,0,nRead));
divFileContent.InnerHtml += "</pre>";
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
<%
...
@ Page language="c#" Codebehind="ShowFile.aspx.cs" AutoEventWireup="false" Inherits="PracticeProject.Practice1.ShowFile"
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > ShowFile </ title >
< meta content ="Microsoft Visual Studio .NET 7.1" name ="GENERATOR" >
< meta content ="C#" name ="CODE_LANGUAGE" >
< meta content ="JavaScript" name ="vs_defaultClientScript" >
< meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
< LINK rel ="stylesheet" type ="text/css" href ="../css/css1.css" >
</ HEAD >
< body >
< form id ="Form1" method ="post" runat ="server" >
< TABLE id ="Table1" cellSpacing ="1" cellPadding ="3" width ="600" bgColor ="#d6d6d6" border ="0"
align ="left" >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 名称: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileName" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 完整目录: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileFullName" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 父目录: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileDirectoryName" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 创建日期: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileCreationTime" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 大小: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileSize" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 访问日期: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileLastAccessTime" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 修改日期: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileLastWriteTime" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" colSpan ="2" >
< DIV id ="divFileContent" style ="DISPLAY: inline; WIDTH: 584px; LINE-HEIGHT: 185%; HEIGHT: 15px"
runat ="server" ms_positioning ="FlowLayout" >< FONT face ="宋体" ></ FONT ></ DIV >
</ TD >
</ TR >
</ TABLE >
</ form >
</ body >
</ HTML >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > ShowFile </ title >
< meta content ="Microsoft Visual Studio .NET 7.1" name ="GENERATOR" >
< meta content ="C#" name ="CODE_LANGUAGE" >
< meta content ="JavaScript" name ="vs_defaultClientScript" >
< meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
< LINK rel ="stylesheet" type ="text/css" href ="../css/css1.css" >
</ HEAD >
< body >
< form id ="Form1" method ="post" runat ="server" >
< TABLE id ="Table1" cellSpacing ="1" cellPadding ="3" width ="600" bgColor ="#d6d6d6" border ="0"
align ="left" >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 名称: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileName" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 完整目录: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileFullName" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 父目录: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileDirectoryName" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 创建日期: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileCreationTime" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 大小: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileSize" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 访问日期: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileLastAccessTime" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" >
< DIV style ="DISPLAY: inline; WIDTH: 63px; HEIGHT: 15px" ms_positioning ="FlowLayout" > 修改日期: </ DIV >
</ TD >
< TD width ="89%" bgColor ="#ffffff" >
< asp:Label id ="lblFileLastWriteTime" runat ="server" ></ asp:Label ></ TD >
</ TR >
< TR >
< TD width ="11%" bgColor ="#ffffff" colSpan ="2" >
< DIV id ="divFileContent" style ="DISPLAY: inline; WIDTH: 584px; LINE-HEIGHT: 185%; HEIGHT: 15px"
runat ="server" ms_positioning ="FlowLayout" >< FONT face ="宋体" ></ FONT ></ DIV >
</ TD >
</ TR >
</ TABLE >
</ form >
</ body >
</ HTML >