Connecting Tech Pros Worldwide Forums | Help | Site Map

writing text to text files in vb. need help

=?Utf-8?B?am9obmFicmFoYW0xMDE=?=
Guest
 
Posts: n/a
#1: Feb 21 '07
hello all,
I've already posted a thread with the same problem but
couldn't satisfactory answer. Let's come to the point.

I can write some text to a text file with WriteAllText method. How can I
start new lines? Example, c:\test.txt is a file, then:

"This text should be written in 1st line of c:\test.txt"
"This text should be written in 2nd line of c:\test.txt"

Please write a that demonstrate the above task.

bye







Robert Dufour
Guest
 
Posts: n/a
#2: Feb 21 '07

re: writing text to text files in vb. need help


Dim MyLine as string
MyLine = "this is line 1" & vbcrlf
however you do append this line to your open text file, next
Myline = "this is line 2" & vbcrlf
however you do append this line to your open text file, next
and so on
HTH
"johnabraham101" <johnabraham101@discussions.microsoft.comwrote in message
news:56861051-9598-40B4-BA85-C3F124293AE1@microsoft.com...
Quote:
hello all,
I've already posted a thread with the same problem but
couldn't satisfactory answer. Let's come to the point.
>
I can write some text to a text file with WriteAllText method. How can I
start new lines? Example, c:\test.txt is a file, then:
>
"This text should be written in 1st line of c:\test.txt"
"This text should be written in 2nd line of c:\test.txt"
>
Please write a that demonstrate the above task.
>
bye
>
>
>
>
>
>

Armin Zingler
Guest
 
Posts: n/a
#3: Feb 21 '07

re: writing text to text files in vb. need help


"johnabraham101" <johnabraham101@discussions.microsoft.comschrieb
Quote:
hello all,
I've already posted a thread with the same problem
but couldn't satisfactory answer. Let's come to the point.
>
I can write some text to a text file with WriteAllText method. How
can I start new lines? Example, c:\test.txt is a file, then:
>
"This text should be written in 1st line of c:\test.txt"
"This text should be written in 2nd line of c:\test.txt"
>
Please write a that demonstrate the above task.
>
bye

http://msdn2.microsoft.com/en-us/library/k3352a4t.aspx

Especially, if "new lines" means appending to a file:
http://msdn2.microsoft.com/en-us/library/3zc0w663.aspx

Also:
http://msdn2.microsoft.com/en-us/lib...b6(VS.71).aspx


Armin
Phill W.
Guest
 
Posts: n/a
#4: Feb 22 '07

re: writing text to text files in vb. need help


johnabraham101 wrote:
Quote:
I've already posted a thread with the same problem but
couldn't satisfactory answer. Let's come to the point.
>
I can write some text to a text file with WriteAllText method.
Short answer - Environment.NewLine will add a line break into your file.

Longer answer -

WriteAllText() is what I call a "convenience method"; it wraps up lots
of other stuff and, so long as it does everything the way you want, all
well and good.
However, I suspect you're reaching a point where it's /not quite/
fitting your requirements any more and you need something else.

Here's another way ...

Dim sw as New StreamWriter( "file", False )

sw.WriteLine( "This text should be written in 1st line of c:\test.txt" )
sw.WriteLine( "This text should be written in 2nd line of c:\test.txt" )

sw.Close()

The StreamWriter class sports other useful methods that may help you in
your file creation as well, for example:

Dim sw as New StreamWriter( "file", False )

For iSub as Integer = 1 to 10
sw.Write( "Line " )
sw.Write( iSub.ToString() )
sw.WriteLine( " of 10" )
Next
sw.Close()

HTH,
Phill W.
Closed Thread