Brief introduction
In the real world, people often want to be able to save some or all of the content of a text file to a Web page variable. In the classic ASP, as long as the simple use of FileSystemObject can be done. In fact, there is a FAQ section in aspfaqs.com to discuss FileSystemObject.
But in the asp.net? FileSystemObject of course, but experience has shown that doing so can seriously affect server performance. Therefore, it is best to use the classes provided by the. NET framework itself to read files. This article is about how to read a text file; I'll also explain how to use the. NET framework classes to read XML files in the future.
File? FileInfo?
In the. NET framework, there are many ways to open a text file. Interestingly, all such methods are concentrated in the two classes in the System.IO namespace. They are the File class and the FileInfo class. The difference between two classes is very small. The methods of the File class are all shared methods (static methods), with the exception of the FileInfo class. Shared methods are methods that can be invoked without creating a class instance. Suppose you want to delete a file with these two classes, you can do this:
'使用 File 类
File.Delete(fileName)
'使用 FileInfo 类
Dim fInfo as FileInfo
fInfo = new FileInfo(fileName)
fInfo.Delete()
Note that the Delete method of the file class takes only one parameter, the file to be deleted, and can be invoked without instantiating the file class. Looking at the FileInfo class again, its Delete method takes no arguments, because the file name is specified in the constructor of the FileInfo class instance.
Personally, I prefer File, so take it as a demonstration. I bet the File class is a little bit more efficient because using it won't bother you because of class instantiation, but that's just guesswork. (If you're interested, you might want to programmatically compare their performance.) When you have finished, please tell me the result! )
Open File
The easiest way to open a text file is to invoke the OpenText method, which opens the text file in UTF-8 (ASCII) format. You can also select the common Open method to specify file operation mode (create/open/append/truncate), Operation Rights (Read/write/read-write), and file share permissions, but since you only need to read the text file, Then the relatively simple OpenText method is enough to deal with.
The OpenText method returns a StreamReader object that can be read from the file that was opened just by accessing the object. Therefore, the ASP.net code used to open the file is roughly as follows:
<%@ Import Namespace="System.IO" %>
<script language="vb" runat="server">
sub Page_Load(sender as Object, e as EventArgs)
'以“只读”方式打开文件
Dim FILENAME as String = Server.MapPath("Rand.txt")
'创建 StreamReader 类实例,准备读取文件内容
Dim objStreamReader as StreamReader
objStreamReader = File.OpenText(FILENAME)
...
Note that the first line of code imports the System.IO namespace. This is because the File class belongs to this namespace. Also, in the Page_Load event handler, a string FILENAME is created, whose contents are the physical path (such as C:\Inetpub\wwwroot\Rand.txt) of the file to be opened. Finally, the Server.MapPath in ASP.net is also the same as the classic ASP (to better understand the Server.MapPath, please do not forget to read in the http://aspnet.4guysfromrolla.com/webtech/ 121799-1.shtml's "Using Server.MapPath").