• 在ASP.NET中获取文件属性


     

    在ASP.NET中获取文件属性(Retrieving File Information In ASP.NET)
    By Steven Smith

    使用ASP.NET我们可以很容易的得到文件的相关信息,包括:文件名、路径、扩展名、大小和创建以及使用日期等。下面,我们就通过一段代码看看如何取得文件的相关信息。

    如果我们使用典型的ASP来获取文件信息,它必须使用.FileSystemObjiect脚本对象来查询文件的有关信息。但在ASP.NET中,.FileSystemObject被System.IO 名称空间取代了,在.System.IO中,它包含了很多获取文件系统信息的类。其中的一个类就是FileInfo,它里面有我们在任何时候都想知道的关于文件的信息。使用这个类(原文使用的是Object),我们可以完全得到文件的许多信息(在这里,我们使用文件自身)。并且使用FileInfo还可以完成对文件的其他一些操作,比如备份、重命名等。

    按照惯例,我们先看代码:

    fileinfo.aspx 
    <%@ Page Language="c#" %>
    <script runat="server">

    protected System.IO.FileInfo objFI;
    protected String filename;

    protected void Page_Load(){
    if(!IsPostBack){
    // 取得文件路径
    filename = Request.ServerVariables["PATH_TRANSLATED"];

    // 创建objFI对象
    objFI = new System.IO.FileInfo(filename);

    // 文件信息
    fullname.Text = objFI.FullName;
    name.Text = objFI.Name;
    size.Text = objFI.Length.ToString();
    created.Text = objFI.CreationTime.ToString();
    accessed.Text = objFI.LastAccessTime.ToString();
    modified.Text = objFI.LastWriteTime.ToString();
    directory.Text = objFI.DirectoryName;
    extension.Text = objFI.Extension;
    }
    }
    </script>
    <HTML>
    <HEAD>
    <link type="text/css" rel="stylesheet" href="http://aspalliance.com/stevesmith/include/ss.css" /></head>
    </HEAD>
    <BODY>
    <form runat="server">
    <b>File Information</b>
    <table class="articlelist">
    <tr>
    <td class="header">Full Name</td>
    <td>
    <asp:Label id="fullname" runat="server"/>
    </td>
    </tr>
    <tr>
    <td class="header">Name</td>
    <td>
    <asp:Label id="name" runat="server"/>
    </td>
    </tr>
    <tr>
    <td class="header">Extension</td>
    <td>
    <asp:Label id="extension" runat="server"/>
    </td>
    </tr>
    <tr>
    <td class="header">Size</td>
    <td>
    <asp:Label id="size" runat="server"/>
    </td>
    </tr>
    <tr>
    <td class="header">Created</td>
    <td>
    <asp:Label id="created" runat="server"/>
    </td>
    </tr>
    <tr>
    <td class="header">Modified</td>
    <td>
    <asp:Label id="modified" runat="server"/>
    </td>
    </tr>
    <tr>
    <td class="header">Accessed</td>
    <td>
    <asp:Label id="accessed" runat="server"/>
    </td>
    </tr>
    <tr>
    <td class="header">Parent Folder</td>
    <td>
    <asp:Label id="directory" runat="server"/>
    </td>
    </tr>
    </table>
    </form>
    </BODY>
    </HTML>

    这段代码非常的浅显,大家很容易就能看明白。首先我们将文件(fileinfo.aspx)的路径赋给变量filename,下一步创建objFI对象,取得我们需要的各属性。通过以上操作,FileInfo类的所有公共属性就可被我们操作了,主要包括以下属性:
    创建日期,路径,扩展名,全名,最后访问时间,修改时间,大小(bytes),文件名
    这样,我们就可以把文件的属性赋给Label控件的Text,把他们显示出来。

  • 相关阅读:
    iTOP-4412开发板-串口基础知识和测试方法
    迅为i.MX6ULL终结者开发板-能想到的功能它都有
    Android4.4.2 源码编译-iMX6Q/D核心板-非设备树源码
    如何让Dev支持c++11特性
    2019年第十届蓝桥杯【C++省赛B组】
    upper_bound()和low_bound函数的基本使用和理解(转载,已获博主授权)
    C++的bitset(位操作使用),转载
    2018年第九届蓝桥杯【C++省赛B组】(未完)
    2013蓝桥杯预赛C/C++本科B组
    信用卡号验证
  • 原文地址:https://www.cnblogs.com/XiangszRN/p/4387097.html
Copyright © 2020-2023  润新知