I need a quick and dirty way to add to a text file in vb.net

I have a very small standalone vb.net application that starts automatically. From time to time, it gets into an error state, which I want to write down, and then continue processing. But this is too small a thing to store in the main log of the system - I just want to add a line to a text file.

What is the least stressful way to add a line of text to a file (and create a file if it's not there) in .net?

+3
source share
5 answers

IO.File.AppendAllText (@ "Y: \ our \ File \ Name.here", "your log message is here")

+19
source

This is in C #, but for a change in VB it should be trivial:

    void logMessage(string message)
    {
        string logFileName = "log.file";

        File.AppendAllText(logFileName,message);
    }

Edited because Joel's solution was much simpler than my =)

+2
source
Private Const LOG_FILE As String = "C:\Your\Log.file"

Private Sub AppendMessageToLog(ByVal message As String)
    If Not File.Exists(LOG_FILE) Then
        File.Create(LOG_FILE)
    End If

    Using writer As StreamWriter = File.AppendText(LOG_FILE)
        writer.WriteLine(message)
    End Using
End Sub
+2
source

This MSDN article, How to Write Text to a File , Should Do It.

+1
source

Source: https://habr.com/ru/post/1699297/


All Articles