By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

writing to a text file

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 06/05/2005 gordon wrote:
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.

--
DaveG - Learning VB.Net slowly
Skoyen - Oslo - Norway
Nov 21 '05 #2

P: n/a
Try using the Writeline method instead of the Write method.
"gordon" <go**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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

Nov 21 '05 #3

P: n/a
thanks Dave but this doesn't seem to work for me. The syntax appears wrong.
"DaveG" <ne*******@daveg.no.nospam> wrote in message
news:CI*******************@news4.e.nsc.no...
On 06/05/2005 gordon wrote:
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.

--
DaveG - Learning VB.Net slowly
Skoyen - Oslo - Norway

Nov 21 '05 #4

P: n/a
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
Nov 21 '05 #5

P: n/a
Gordon,
thanks Dave but this doesn't seem to work for me. The syntax appears
wrong.

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.

Can you show what you made from the sample Dave give you.

Cor
Nov 21 '05 #6

P: n/a
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
Nov 21 '05 #7

P: n/a
On 06/05/2005 Cor Ligthert wrote:
Gordon,
thanks Dave but this doesn't seem to work for me. The syntax
appears wrong.

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.

Can you show what you made from the sample Dave give you.

Cor


Gordon are you using:

Imports system.io

If not it will give a syntax error

--
DaveG - Learning VB.Net slowly.
Skoyen - Oslo - Norway
Nov 21 '05 #8

P: n/a
Thank you all for your kind assistance.

I went with Crouchie1988's suggestion. This drops to the next line and
clears the buffer.
"Crouchie1998" <cr**********@spamcop.net> wrote in message
news:Ox*************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.