【VB】文字檔-寫檔及讀檔-II

'讀取文字檔
Function GetTextString(ByVal file_name As String) As String
        Dim value As String = ""
        Dim MySF As IO.StreamReader = New IO.StreamReader(file_name, System.Text.Encoding.Default)

        'value = MySF.ReadToEnd() '讀取全部會包含換行符號
        Do While Not MySF.EndOfStream
            value &= MySF.ReadLine '讀取單行
        Loop
        MySF.Dispose()
        Return value
End Function

 '讀檔呼叫
 Dim url_txt As String = GetTextString(Server.MapPath(Get_Val("upload")) & "edm.txt")

'寫檔
  Dim MySW As IO.StreamWriter = New IO.StreamWriter(Server.MapPath(Get_Val("upload")) & "edm.txt", True, System.Text.Encoding.Default)
  MySW.WriteLine("歡迎光臨~Jason blog")    '換行寫入
  MySW.WriteLine("有任何問題~請多多指教~~")
  MySW.Write("謝謝!感恩")  '不換行寫入
  MySW.Dispose()

EX: 上面文字檔內容 如下
    歡迎光臨~Jason blog
    有任何問題~請多多指教~~謝謝!感恩

PS:建議採用這個方式,比較不會出現檔案寫入權限方面的問題

留言

這個網誌中的熱門文章

【ASP】日期轉換(西元<-->民國)

【VB】使用NPOI元件來匯出Excel--DataTableToExcel

【SQL】符號切割字串變成多欄