Asp.net| is created in ASP.net, the whole process of file processing revolves around the namespace of System.IO. This namespace has classes that are required to perform file reads and writes. Starting with the most basic operation, this article explains the concept of file handling in ASP.net, including reading from a file, writing to a file, and deleting a file.
As mentioned earlier, there must be a "System.IO" namespace for file processing in the ASP.net page. So the first step is to introduce this namespace:
<%@ Import namespace= "System.IO"%>
The next step is to create a text file and assign the text file to a stream writing object so that you can write to the text file. Use the following code to complete this task:
Writefile.aspx
<%@ Import namespace= "System.IO"%>
<%
Response.Write ("Writing the content into Text File in asp.net<br>")
The Declare stream writing object
Dim Strwriterobj as StreamWriter
"Create a text file, assign a Textfile object
strwriterobj= file.createtext ("C:aspnet.txt")
The Write content
Strwriterobj. WriteLine ("Welcome to wonderfull world of asp.net Programming") "
Completes the operation, closes the stream object
Strwriterobj. Close
Response.Write ("Done with the creation of text file and writing content into it")
%>
It's done! Now let's move on to the next task and read the content from the text file we just created.
Reading content from a file
Reading from a file is roughly the same as writing to a file, but be aware of the following two things:
1. file reading using StreamReader Class 2. When the ReadLine method is used, the end of the text file that will be read is marked with an empty string ("").
Now start writing code to read from the Aspnet.txt file you created earlier:
Readfile.aspx
<%@ Import namespace= "System.IO"%>
<%
Response.Write ("Reading" the content from the text file ASPNET.) Txt<br> ")
The Create stream Read object
Dim Streamreaderobj as StreamReader
"Declares a variable to hold the content read from the file
Dim Filecont as String
"Open text file, assign to stream read object
Streamreaderobj = File.OpenText ("C:aspnet.txt")
"Read the contents of a file line by row
Todo
Filecont = Streamreaderobj. ReadLine ()
Response.Write (Filecont & "<br>")
Loop Until Filecont = ""
"Closes the stream read object after the read operation completes
Streamreaderobj. Close
Response.Write ("<br>done with reading the content from the file Aspnet.txt")
%>
deleting files
Deleting files in asp.net is also very simple and intuitive. The "File" class in the System.IO namespace has a Delete method that deletes the file, passing the file name as an argument. The following code demonstrates the steps for file deletion in asp.net:
Filedelete.aspx
<%@ Import namespace= "System.IO"%>
<%
File.delete ("C:aspnet.txt")
Response.Write ("The File ASPNET is deleted successfully!!!")
%>
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.