This article explains the basic operations in ASP. net, including reading content from a file, writing content to a file, and deleting a file.
As mentioned above, to process files on the ASP. NET page, you must have a "system. Io" namespace. Therefore, the first step is to introduce the namespace:
<% @ Import namespace = "system. Io" %>
The next step is to create a text file and allocate it to a stream writing object so that you can write content to the text file. Use the following sectionCodeTo complete this task:
Writefile. aspx
<% @ Import namespace = "system. Io" %>
<%
Response. Write ("writing the content into text file in ASP. NET <br> ")
"Declaring stream writing objects
Dim strwriterobj as streamwriter
"Create a text file and assign textfile objects
Strwriterobj = file. createtext ("C: aspnet.txt ")
"Write content
Strwriterobj. writeline ("Welcome to wonderfull world of ASP. NET programming ")"
Complete the operation to close the Stream Object
Strwriterobj. Close
Response. Write ("done with the creation of text file and writing content into it ")
%>
This is done! Now let's proceed to the next task and read the content from the text file just created.
Read content from a file
Reading content from a file is roughly the same as writing content to the file, but pay attention to the following two things:
1. Use the streamreader class for File Reading
2. When the Readline method is used, an empty string ("") is used to mark the end of the text file to be read.
The write code is now used to read the content from the aspnet.txt file created earlier:
In ASP. NET, the entire process of file processing is centered around the namespace system. Io. This namespace contains classes required for reading and writing execution files.
Readfile. aspx
<% @ Import namespace = "system. Io" %>
<%
Response. Write ("reading the content from the text file ASPnet. txt <br> ")
"Create a stream read object
Dim streamreaderobj as streamreader
"Declare variables to store the content read from the file
Dim filecont as string
"Open a text file and assign it to the stream reading object.
Streamreaderobj = file. opentext ("C: aspnet.txt ")
"Reading file content row by row
Do
Filecont = streamreaderobj. Readline ()
Response. Write (filecont & "<br> ")
Loop until filecont = ""
"Closes the stream reading object after the read operation is completed.
Streamreaderobj. Close
Response. Write ("<br> done with reading the content from the file aspnet.txt ")
%>
Delete an object
Deleting files in ASP. NET is also very simple and intuitive. The "file" (File) Class in the system. Io namespace has a delete method to delete files, which uses the file name as an independent variable for transmission. The following code demonstrates how to delete a file in ASP. NET:
Filedelete. aspx
<% @ Import namespace = "system. Io" %>
<%
File. Delete ("C: aspnet.txt ")
Response. Write ("the file ASPnet is deleted successfully !!! ")
%>