473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

writing to a text file

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
8 1287
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
2
by: Tony | last post by:
Yes, I need to specify a font type so that the characters will be evenly spaced when I write to a tab delimited text file. So how does one specify a font type to write/print and which font is...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
5
by: grinder | last post by:
first off, i am an extreme newbie to C. i am an undergrad research assistant and i have been shifted to a project that involves building a fairly involved c program. The part that i am stuck on now...
2
by: macdalor | last post by:
I'm trying to add a guestbook to my website and got these scripts from the net (http://www.phpeasystep.com/workshopview.php?id=15) but only 1d and date&time are being recorded in the sql db; name,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.