目录文件操作实例[3]_使用文件预览显示文件信息

在此实例中仍需要先将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 窗体设计器生成的代码
    }

}

<% @ 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 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值