1. Create a file and write the content
Remember to import the namespace Imports System. IO
Sub CreateWriteFile () 'create a file
Response. Write ("Writing the content into Text File in ASP. NET <BR> ")
'Declare streamwriter object
Dim strwriterobj As StreamWriter
'Create a text file and assign it to the streamwriter object declared above.
Strwriterobj = File. CreateText ("c: \ aspnet.txt ")
'Write some stuff in the text file just created
Strwriterobj. WriteLine ("Welcome to user chenyang's ASP. NET Program ")
Strwriterobj. Close ()
Response. Write ("Create a text file and fill in the content ")
End Sub
2. Read File Content
Sub ReadFile ()'
Response. Write ("reading the content in the asp.net file <br> ")
'Declare A StreamReader object
Dim streamreaderobj As StreamReader
'Declare a variable to save the read content
Dim filecont As String
'Open the file and assign it to the StreamReader object.
Streamreaderobj = File. OpenText ("c: \ aspnet.txt ")
'Reading file content cyclically by row
Do
Filecont = streamreaderobj. ReadLine ()
If filecont = "" Then
Response. Write (filecont)
Else
Response. Write (filecont & "<br> ")
End If
Loop Until filecont = ""
'Close StreamReader object
Streamreaderobj. Close ()
Response. Write ("<br> read the content of the aspnet.txt file ")
End Sub
3. delete an object
Sub DeleteFile () 'delete an object
File. Delete ("c: \ aspnet.txt ")
Response. Write ("the file is deleted successfully !!! ")
End Sub
4. upload files
Attachment <a name = "anchor1"> </a>
<Asp: FileUpload ID = "FileUpload1" runat = "server" TabIndex = "4" Width = "300px"/>
<Asp: Button ID = "cmdSaveAttachment" runat = "server" CssClass = "button" TabIndex = "5"
Text = "Save attachments"/>
Protected Sub saveAttachment ()
If FileUpload1.HasFile Then
'Retrieve the website root directory path
Dim path As String = HttpContext. Current. Request. MapPath ("../UploadFile /")
FileUpload1.SaveAs (path & FileUpload1.FileName)
Else
Response. Write ("<script language = 'javascript '> alert ('select attachment First'); </script> ")
End If
End Sub
5. Download the attachment
Add the address connection to the text.
Label1.Text = "<a href = '../UploadFile/TextFile.txt' target = '_ blank'> TextFile.txt </a> & nbsp ;"