TheTripleDeuce
TheTripleDeuce

Reputation: 103

edit text file and save text file with edits

ok so the easiest way i can break down what im trying to do is this, I have a text file with 5 words on it (maybe more in the future), one line has 3 words and one line has 2 words, all words separated by spaces

I'm trying to load the words in the text file into a listbox BUT I only want one word per line in the list box.

i don't have any sample code as I cannot for the life of me figure this out.

the text file looks like:

account1 account2 account3
account4 account5

I want it to look like(one word per line):

account1 
account2 
account3 
account4 
account
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         Dim lines As String() = IO.File.ReadAllLines("C:\Scrape\textfile.txt")             ListBox1.Items.AddRange(lines)         
    Dim fw As New IO.StreamWriter("C:\Scrape\textfile.txt")         
    For Each itm As Object In Me.ListBox1.Items             
        fw.WriteLine(itm.ToString)         
    Next         
    fw.Close()
        fw.Dispose()
        fw = Nothing
        For Each line In lines             
        String.Split()         
    Next     
End Sub

any help is greatly appreciated I have been trying to figure this out for about 4 hours :'(

final working code below for those interested

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.Items.AddRange(IO.File.ReadAllText("C:\Scrape\textfile.txt").Replace(vbLf, "").Replace(" ", vbCr).Split(vbCr, options:=StringSplitOptions.RemoveEmptyEntries))

        ' Read
        'TextBox1.Multiline = True
        ListBox1.Text = IO.File.ReadAllText("C:\Scrape\textfile.txt")


        'Write
        'IO.File.WriteAllText("C:\Scrape\textfile.txt", ListBox1.Text)
        Dim SW As IO.StreamWriter = IO.File.CreateText("C:\Scrape\textfile.txt")
        For Each S As String In ListBox1.Items
            SW.WriteLine(S)
        Next
        SW.Close()
    End Sub

Upvotes: 0

Views: 464

Answers (2)

mandarin software
mandarin software

Reputation: 217

For editing :
 Try
     Dim thefile As String = "C:\con_ip.txt" 'Put your path
     Dim lines() As String = System.IO.File.ReadAllLines("C:\con_ip.txt")
     lines(Put which line to edit) = "Put the Value to edit "
     System.IO.File.WriteAllLines(thefile, lines)
 Catch ex As Exception

 End Try

Upvotes: 0

Nick Abbot
Nick Abbot

Reputation: 501

    ListBox1.Items.AddRange(IO.File.ReadAllText("C:\File.txt").Replace(vbLf, "").Replace(" ", vbCr).Split(vbCr, options:=StringSplitOptions.RemoveEmptyEntries))

    ' Read
    TextBox1.Multiline = True
    TextBox1.Text = IO.File.ReadAllText("C:\File.txt")


    'Write
    IO.File.WriteAllText("C:\File.txt", TextBox1.Text)

Upvotes: 1

Related Questions