❗ Create New Text File from List Box

Posts 1–9 of 9 · Page 1 of 1
❗ Create New Text File from List Box
Anyone know the code for creating a new text file containing the items in a list box? i know how to write into a existing textfile but how do you create a new one? preferably including the ability to select file destination
Quote Originally Posted by Biesi View Post
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

Code:
Dim contents As New List(Of String)
For Each itm As Object In ListBox1.Items
    contents.Add(itm.ToString())
Next
File.WriteAllLines("path.txt", contents.ToArray())
Something like this maybe
the link cant be opened... what was it about?
You'll need to import the 'IO' namepsace.

Code:
Imports System . IO
The following will show a save file dialog where you can select where to save the text file to. The save part is from the post above.

Code:
Using SFD As New SaveFileDialog
    If SFD.ShowDialog() = DialogResult.Ok Then
        SFD.Filter = "Text File (*.txt) | *.txt"
        Dim Contents As New List(Of String)
        For Each Item As Object In ListBox1.Items
            contents.Add(Item.ToString())
        Next
        File.WriteAllLines(SFD.FileName, Contents.ToArray())
    End If
End Using
alright i know you will hate me for this but can you please explain the code? (im a noob)
Quote Originally Posted by Geometrical View Post
You'll need to import the 'IO' namepsace.

Code:
Imports System . IO
The following will show a save file dialog where you can select where to save the text file to. The save part is from the post above.

Code:
Using SFD As New SaveFileDialog
    If SFD.ShowDialog() = DialogResult.Ok Then
        SFD.Filter = "Text File (*.txt) | *.txt"
        Dim Contents As New List(Of String)
        For Each Item As Object In ListBox1.Items
            contents.Add(Item.ToString())
        Next
        File.WriteAllLines(SFD.FileName, Contents.ToArray())
    End If
End Using
i copied and pasted your code what does "file" mean at File.WriteAllLines(SFD.FileName, Contents.ToArray())? and whats a importsystem****?

noob question im ashamed
I DID IT!!!
using this code
================================================== ===============================================
Dim saveFileDialog1 As New SaveFileDialog()

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True

If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Using sw As New IO.StreamWriter(saveFileDialog1.FileName, False)
'write each row of the ListView out to a tab-delimited line in a file
For i As Integer = 0 To Me.List.Items.Count - 1
sw.WriteLine(List.Items.Item(i))
Next
End Using
End If
================================================== =========================================
Quote Originally Posted by muteninja View Post
i copied and pasted your code what does "file" mean at File.WriteAllLines(SFD.FileName, Contents.ToArray())? and whats a importsystem****?

noob question im ashamed
IO is a namespace in System, File is a class in it that contains functions to manipulate files.
And the link was about the IO.File class - showing and explaining all functions
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us