Add text to file

S

Stijn

Hello

I'm new to vb.net. Can someone help me with this one? I'm trying to add text
to an existing file. The function has to add text at the end of an existing
text-file. The code I've got so far writes the text to the file. But it
overwrites the existing text in the file. What do I have to change to the
code to add text instead of overwriting?

The code:

Sub LogFoutmelding(ByVal strFoutmelding As String, ByVal strApplicatie As
String)

'Dim strPath = System.IO.Directory.GetCurrentDirectory()

Dim strErrorlog As String = "errorlog.txt"

Dim strDatum As String = System.DateTime.Now.Date.ToString

Dim strTijdstip As String = System.DateTime.Now.TimeOfDay.ToString

Dim strTsslijn As String = "-----"

Try

FileOpen(1, strErrorlog, OpenMode.Output)

PrintLine(1, strDatum)

PrintLine(1, strTijdstip)

PrintLine(1, strApplicatie)

PrintLine(1, strFoutmelding)

PrintLine(1, strTsslijn)

FileClose(1)

Catch ex As Exception

MessageBox.Show(ex.ToString)

End Try

End Sub

Thx in advance
Stijn
 
C

Cor

Hallo Stijn,

Have a look at the next topics
in/on MSDN "streamreader" and "streamwriter" Class (you have than also your
examples) there is also an append method there and also have a look at the
OpenFileDialog and CloseFileDialog

You are now using the old VB art, nothing wrong with, but the methods I
called are more descripting as least.

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

* "Stijn said:
Hello

I'm new to vb.net. Can someone help me with this one? I'm trying to add text
to an existing file. The function has to add text at the end of an existing
text-file. The code I've got so far writes the text to the file. But it
overwrites the existing text in the file. What do I have to change to the
code to add text instead of overwriting?

Try to open the file with 'OpenMode.Append'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top