使用VB.NET,我试图创建一个文本文件,如果它不存在或附加文本(如果存在)。
由于某些原因,尽管它正在创建文本文件,但我收到一个错误,表示进程无法访问文件。
当我运行程序时,它正在编写文本,但是如何使它在新行上写?
Dim strFile As String = "C:\ErrorLog_" & DateTime.Today.ToString("dd-MMM-yyyy") & ".txt" Dim sw As StreamWriter Dim fs As FileStream = Nothing If (Not File.Exists(strFile)) Then Try fs = File.Create(strFile) sw = File.AppendText(strFile) sw.WriteLine("Start Error Log for today") Catch ex As Exception MsgBox("Error Creating Log File") End Try Else sw = File.AppendText(strFile) sw.WriteLine("Error Message in Occured at-- " & DateTime.Now) sw.Close() End If
尝试这个:
原文链接:https://www.f2er.com/vb/256034.htmlDim strFile As String = "yourfile.txt" Dim fileExists As Boolean = File.Exists(strFile) Using sw As New StreamWriter(File.Open(strFile,FileMode.OpenOrCreate)) sw.WriteLine( _ IIf(fileExists,_ "Error Message in Occured at-- " & DateTime.Now,_ "Start Error Log for today")) End Using