ASP. NET file operations create read Delete upload download

Source: Internet
Author: User

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 ;"

 

 

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.