Writing listbox items into text file

K

kimiraikkonen

Hi,
I can read from a text file and insert text file's contents into a
listbox whose format is line by line using this code:

Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("c:
\bookmarks.txt")

Dim strs() As String
strs = Split(reader, Environment.NewLine)


For Each s As String In strs
ListBox1.Items.Add(s)
Next


But i also want to write all listbox items into text file "line by
line"? How can i do this?

I tried My.Computer.FileSystem.WriteAlltext but couldn't make out
completely.

I hope i learn.

Thanks!
 
K

Kerem Gümrükcü

Hi kimiraikkonen,

use this if you like:
TextWriter.WriteLine Method.

Check the Stream Classes and their derivates
that come with write line method. You also can
use plain streams of bytes and byte arrays with
your own line feeds and breaks. But if you dont
want to implement this use the already available
write line methods,...

Regards

Kerem

--
 
H

Herfried K. Wagner [MVP]

kimiraikkonen said:
I can read from a text file and insert text file's contents into a
listbox whose format is line by line using this code:

Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("c:
\bookmarks.txt")

Dim strs() As String
strs = Split(reader, Environment.NewLine)


For Each s As String In strs
ListBox1.Items.Add(s)
Next

=> 'ListBox1.Items.AddRange(strs)'.
But i also want to write all listbox items into text file "line by
line"? How can i do this?

I tried My.Computer.FileSystem.WriteAlltext but couldn't make out
completely.

Take a look at the 'Join' function.
 
K

kimiraikkonen

Hi kimiraikkonen,

use this if you like:
TextWriter.WriteLine Method.

Check the Stream Classes and their derivates
that come with write line method. You also can
use plain streams of bytes and byte arrays with
your own line feeds and breaks. But if you dont
want to implement this use the already available
write line methods,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space:http://kerem-g.spaces.live.com/
Latest Open-Source Projects:http://entwicklung.junetz.de

Please sample some code. There must be some ways to save all listbox
items as a text file using with my.computer.filesystem.writealltext
classes instead of using Stream Writer's "writeline" method.

I tried something with "for each" but it only saves one(last) item if
append is false.

For Each i As String In ListBox1.Items
My.Computer.FileSystem.WriteAllText("c:\test.txt",
environment.newline & i, False)
Next

If append is true, previous + current items are saved into text file.
But i want current (last updated) items to be saved in a text file.
Shorly, i want to save what and how i see in listbox items as text
file.
 
K

kimiraikkonen

=> 'ListBox1.Items.AddRange(strs)'.



Take a look at the 'Join' function.

Could you help to code using "join" to save all listbox items in a
textfile line by line?

Thanks
 
H

Herfried K. Wagner [MVP]

kimiraikkonen said:
Could you help to code using "join" to save all listbox items in a
textfile line by line?

Untested:

\\\
Dim ItemArray(Me.ListBox1.Items.Count - 1) As Object
Me.ListBox1.Items.CopyTo(ItemArray, 0)
Dim Data As String = Join(ItemArray, Environment.NewLine)
File.WriteAllText(<path>, Data)
///
 
K

kimiraikkonen

Untested:

\\\
Dim ItemArray(Me.ListBox1.Items.Count - 1) As Object
Me.ListBox1.Items.CopyTo(ItemArray, 0)
Dim Data As String = Join(ItemArray, Environment.NewLine)
File.WriteAllText(<path>, Data)
///

Modified as:

Dim ItemArray(Me.ListBox1.Items.Count - 1) As Object
Me.ListBox1.Items.CopyTo(ItemArray, 0)
Dim Data As String = Join(ItemArray, Environment.NewLine)
My.Computer.FileSystem.WriteAllText("c:\test.txt", Data, False)

and worked! Thanks.
 

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