Getting file properties in asp.net

Source: Internet
Author: User
asp.net gets the file attributes (retrieving file information in ASP) in asp.net. NET)
by Steven Smith

With asp.net we can easily get information about the file, including file name, path, extension, size and creation, and date of use. Next, we'll take a piece of code to see how to get information about the file.

If we use a typical ASP to get file information, it must be used. Filesystemobjiect the Script object to query for information about the file. But in ASP.net,. FileSystemObject is replaced by the System.IO namespace, in. System.IO, which contains a lot of classes to get file system information. One of these is FileInfo, which has information about the file that we want to know at all times. Using this class (object in the original text), we can completely get a lot of information about the file (here we use the file itself). You can also use FileInfo to perform other operations on the file, such as backup, renaming, and so on.

By convention, let's look at the code first:

Fileinfo.aspx
<%@ Page language= "C #"%>
<script runat= "Server" >

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

protected void Page_Load () {
if (! IsPostBack) {
Get file path
filename = request.servervariables["path_translated"];

Creating Objfi Objects
Objfi = new System.IO.FileInfo (filename);

File information
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>
<BODY>
<form runat= "Server" >
<b>file information</b>
<table class= "Articlelist" >
<tr>
&LT;TD class= "header" >full name</td>
<td>
<asp:label id= "FullName" runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD class= "Header" >Name</td>
<td>
<asp:label id= "name" runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD class= "Header" >Extension</td>
<td>
<asp:label id= "extension" runat= "server"/>
</td>
</tr>
<tr>
&LT;TD class= "Header" >Size</td>
<td>
<asp:label id= "Size" runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD class= "Header" >Created</td>
<td>
<asp:label id= "created" runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD class= "Header" >Modified</td>
<td>
<asp:label id= "Modified" runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD class= "Header" >Accessed</td>
<td>
<asp:label id= "Accessed" runat= "Server"/>
</td>
</tr>
<tr>
&LT;TD class= "header" >parent folder</td>
<td>
<asp:label id= "directory" runat= "Server"/>
</td>
</tr>
</table>
</form>
</BODY>
</HTML>

This code is very shallow, it is easy to see. First we assign the path of the file (fileinfo.aspx) to the variable filename, and the next step is to create the Objfi object and get the attributes we need. By doing so, all public properties of the FileInfo class can be manipulated by us, including the following properties:
Creation date, path, extension, full name, last access time, modified time, size (bytes), filename
In this way, we can assign the properties of the file to the text of the label control and display them.

Translation: CNet


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.