利用SYSTEM.IO 名空間中的STREAMWRITER,STREAMREADER及FILE類,完成文件讀、寫、刪除的操作。
--------------------------------------------------------------------------------
1、寫文件
writefile.aspx
<%@ Import Namespace="System.IO" %> '引入所需的NameSpace
<%
Response.write("Writing the content into Text File in ASP.NET <BR>")
Dim strwriterobj As StreamWriter '聲明一個StreamWriter對象
strwriterobj= File.CreateText("c:/aspnet.txt") '新建一個文本文件,賦值給StreamWriter對象
strwriterobj.WriteLine( "Welcome to wonderfull world of ASP.NET Programming" )
'向文件中寫內容
strwriterobj.Close '關閉對象
Response.write("Done with the creation of text file and writing content into it")
%>
2、讀文件
readfile.aspx
<%@ Import Namespace="System.IO" %>
<%
Response.write("Reading the content from the text file ASPNET.TXT
")
Dim streamreaderobj As StreamReader '聲明一個StreamReader對象
Dim filecont As String '聲明一個變量保存讀出的內容
streamreaderobj = File.OpenText( "c:/aspnet.txt" ) '打開文件賦值到StreamReader對象
Do '按行循環讀取文件內容
filecont = streamreaderobj.ReadLine()
Response.Write( filecont & "
" )
Loop Until filecont = ""
streamreaderobj.Close '關閉StreamReader對象
Response.write("
Done with reading the content from the file aspnet.txt")
%>
3、刪除文件
Filedelete.aspx
<%@ Import Namespace="System.IO" %>
<%
File.Delete("c:/aspnet.txt" ) '刪除文件
Response.write("The File aspnet is deleted successfully !!!" )
%>