Read text files on ASP. NET pages

Source: Internet
Author: User
Tags classic asp
Introduction
In the real world, people often want to save part or all of a text file to a Web page variable. In the classic ASP, you can simply use FileSystemObject. In fact, in ASPFAQs.com, there is a FAQ topic dedicated to discussing FileSystemObject.
But in ASP. NET? FileSystemObject can also be used. However, experience shows that this will seriously affect server performance. Therefore, it is best to use the class provided by the. NET Framework itself to read files. This article describes how to read text files, and how to use the. NET Framework class to read XML files.
File? FileInfo?
In the. NET framework, there are many ways to open text files. Interestingly, all such methods are concentrated in the two classes of the System. IO namespace. They are File Class and FileInfo class. The difference between the two classes is very small. All methods of the File class are shared methods (static methods), with the exception of the FileInfo class. The shared method is a method that can be called without creating a class instance. If you want to delete files using these two classes, you can do this:
'Use the File class
File. Delete (fileName)
'Use FileInfo class
Dim fInfo as FileInfo
FInfo = new FileInfo (fileName)
FInfo. Delete ()
Note that the Delete method of the File class only includes one parameter, that is, the File to be deleted, and can be called without instantiating the File class. Let's take a look at the FileInfo class. Its Delete method does not contain parameters because the file name is specified in the constructor of the FileInfo class instance.
Personally, I prefer File, so let's use it for demonstration. I bet that the efficiency of the File class is a little higher, because using it won't worry about class instantiation; but it's just a guess. (If you are interested, program and compare their performance. After you finish, please tell me the result !)
Open a file
The easiest way to open a text file is to call the OpenText method, which will open the text file in UTF-8 (ASCII) format. You can also select a common Open method to specify the file operation mode (create/open/append/truncate) and Operation permissions (read/write/read-write) share permissions with files. However, since you only need to read text files, the relatively simple OpenText method is sufficient.
Related Article

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.