writing to a text file

  • Thread starter Thread starter gordon
  • Start date Start date
G

gordon

Hi

I am learning vb.net and am trying to write information to a text file or to
append to that file if it already exists.

I am using the following code to write to the file, however the text is
always being replaced. Please let me know if there is a better way of doing
this.
Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim file As New System.IO.StreamWriter("c:\test.txt")

Dim name As String = TextBox1.Text

Dim value As String = TextBox2.Text

file.Write("Hello " & name & " You entered the value of " & value)

file.Close()

End Sub



Thanks

Doug
 
Dim file As New System.IO.StreamWriter("c:\test.txt")

Hi

Try typing "appending" into the help index screen, the help files give
a good example there.

Dim w As StreamWriter = File.AppendText("log.txt")

I'm no experienced VB programmer but I find the help files fantastic
help and first stop.
 
thanks Dave but this doesn't seem to work for me. The syntax appears wrong.
 
Gordon,

This should work

///
Dim file As New System.IO.StreamWriter("c:\test.txt", True)

Dim name As String = TextBox1.Text
Dim value As String = TextBox2.Text

file.Write("Hello " & name & " You entered the value of " & value)
file.Close()
\\\

HTH
 
Gordon,
thanks Dave but this doesn't seem to work for me. The syntax appears
wrong.
Can you show what you made from the sample Dave give you.

Cor
 
Dim sw As New IO.StreamWriter("C:\MyTestFile.txt", True) ' True means
'append'
Dim strName As String = TextBox1.Text
Dim strValue As String = TextBox2.Text
sw.WriteLine(String.Format("Hello {0}, you entered the value of {1}",
strName, strValue)
sw.Flush() ' Always flush the buffer when finished writing
sw.Close()


I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
 

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

Back
Top