1.建立檔案並寫入內容
記得匯入命名空間Imports System.IO
Sub CreateWriteFile() '建立檔案
Response.Write("Writing the content into Text File in ASP.NET <BR>")
' 聲明streamwriter對象
Dim strwriterobj As StreamWriter
'建立文字檔並將其賦給上面聲明的streamwriter對象
strwriterobj = File.CreateText("c:\aspnet.txt")
'在剛才建立的文字檔裡寫一些東東
strwriterobj.WriteLine("Welcome to user chenyang's ASP.NET Program")
strwriterobj.Close()
Response.Write("建立文字檔並填充內容")
End Sub
2.讀取檔案內容
Sub ReadFile() '讀取檔案
Response.Write("讀取asp.net檔案中的內容 <br>")
'聲明一個StreamReader對象
Dim streamreaderobj As StreamReader
'聲明一個變數儲存讀出的內容
Dim filecont As String
'開啟檔案賦值到StreamReader對象
streamreaderobj = File.OpenText("c:\aspnet.txt")
'按行迴圈讀取檔案內容
Do
filecont = streamreaderobj.ReadLine()
If filecont = "" Then
Response.Write(filecont)
Else
Response.Write(filecont & "<br>")
End If
Loop Until filecont = ""
'關閉StreamReader對象
streamreaderobj.Close()
Response.Write("<br> 已經讀完aspnet.txt檔案的內容")
End Sub
3.刪除檔案
Sub DeleteFile() '刪除檔案
File.Delete("c:\aspnet.txt")
Response.Write("檔案刪除成功!!!")
End Sub
4.上傳檔案
附件<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="儲存附件" />
Protected Sub saveAttachment()
If FileUpload1.HasFile Then
'擷取網站根目錄路徑
Dim path As String = HttpContext.Current.Request.MapPath("../UploadFile/")
FileUpload1.SaveAs(path & FileUpload1.FileName)
Else
Response.Write("<script language='javascript'>alert('請先選擇附件 ');</script>")
End If
End Sub
5.下載附件
在文本上加上地址串連即可
Label1.Text = "<a href=' ../UploadFile/TextFile.txt ' target='_blank'> TextFile.txt </a> "