目录文件操作实例[3]_使用文件预览显示文件信息
来源:互联网 发布:路由端口统计 编辑:程序博客网 时间:2024/06/10 03:25
在此实例中仍需要先将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>
- 目录文件操作实例[3]_使用文件预览显示文件信息
- 文件操作之3_扫描目录
- 使用linux c 目录操作函数,读取任一目录下的所有目录信息文件信息
- 层级显示文件目录结构信息案例
- 学习笔记_操作文件和目录
- 目录文件操作实例[2]_列出当前盘(驱动器)上的所有目录及文件
- AIR文件操作:使用文件对象操作文件和目录 .
- AIR-文件操作:使用文件对象操作文件和目录
- AIR-文件操作:使用文件对象操作文件和目录
- C#_创建Excel文件实例操作
- 目录文件操作实例[1]_列出本地硬盘上的所有驱动器
- C# 对包含文件或目录路径信息的 System.String 实例执行操作
- C#文件、文件目录操作
- 递归显示文件目录
- QT 显示文件目录
- 显示文件目录
- 显示隐藏目录文件
- nginx 显示文件目录
- 关于内存对齐的学习笔记
- 类模板的声名
- 多态性、虚拟方法调用
- 格莱姆.奥波利:平民英雄
- Zee脚本练习之六:LR中real协议的简易脚本展示
- 目录文件操作实例[3]_使用文件预览显示文件信息
- 最基本的Socket编程 C#版 - 星际之家 - 博客园
- composite 模式
- Zee之LR脚本练习之七:执行dir命令并把结果写到文件里
- 国内各大银行软件开发中心情况汇总
- 我来清华一年策划出版的11本书
- UTF-8转换成Shift_jis,识别非Shift_jis字符,如(~、①等)
- mysql字符问题和连接太多的问题
- 打算写一款spice电路仿真程序