Link to home
Start Free TrialLog in
Avatar of bassendoughboy
bassendoughboy

asked on

append to file

I would like to open a file and add to the file the information that is in a listbox, here is what i have, but it puts in all one one line need each line to be a return, I also dont want to over write whats there



Private Sub btnaddfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddfile.Click

        Dim mystream As IO.FileStream
        Dim mywright As StreamWriter
        Dim inti As Integer

        OFDADD.FileName = txtname.Text
        OFDADD.ShowDialog()

        mystream = New IO.FileStream(OFDADD.FileName, FileMode.Append, IO.FileAccess.Write)
        mywright = New IO.StreamWriter(mystream)
        mywright.WriteLine("lstHitLog.text")
        For inti = 0 To lstHitLog.Items.Count - 1
            lstHitLog.SelectedIndex = inti
            mywright.Write(lstHitLog.Text)
        Next
        mywright.Close()  

    End Sub
Avatar of softplus
softplus

Hi bassendoughboy,
Easy: just replace mywright.Write(.. with mywright.WriteLine(...  - like you did just above the for-loop.
John
Avatar of Mike Tomlinson
You don't need the FileStream.  Also you should be checking the result of your dialog before proceeding:

    Private Sub btnaddfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddfile.Click
        Dim mywright As IO.StreamWriter
        Dim item As String

        OFDADD.FileName = txtname.Text
        If OFDADD.ShowDialog() = DialogResult.OK Then
            mywright = New IO.StreamWriter(OFDADD.FileName, True)
            mywright.WriteLine("lstHitLog.text")
            For Each item In lstHitLog.Items
                mywright.WriteLine(item)
            Next
            mywright.Close()
        End If
    End Sub
Good one, Mike, since the StreamWriter has so many overloaded methods, it's hard to find the one that says:

5 of 7 New(path As String, append As Boolean)

Bob
this should do it, there is no need for the streamreader if you just want to append

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim mywright As StreamWriter
        mywright = New IO.StreamWriter("c:\headers.txt", True)
        For Each x As Object In ListBox1.Items
            mywright.WriteLine(x.ToString)
        Next
        mywright.Close()

    End Sub
Way to late :-)
Good morning TheLearnedOne and Idle_Mind :-)
Good afternoon, Ronald =:)

Bob
Avatar of bassendoughboy

ASKER

Oh was that not easy thanks for your help, not sure if I should start a new question or not, I would also like to have it check to see if file exists if so add to it if not make a new one
I need coffee and the kids have the day off from school...but good morning to all as well.    ;)
When you open in Append mode as in my example it will create or append to the file as necessary.
I had a line i did not need "mywright.WriteLine("lstHitLog.text")"
when I use the open file dialog it will add to it but not make a new file, when I use save file dialog it will make new one but not add to it how can i combine the two to where it will eather add to the file or creat a new one
You don't need an OpenFileDialog and should only be using the SaveFileDialog.

Do you want "lstHitLog.text" only at the top of the file...or not in the file at all?
ok I see now, when i had the other line in it would add "lstHitLog.text"  into the file also , did not need that, I only what was added to the listbox,When I save the file it ask to over write , I click ok and then it adds to the file, any way arould this?
If you only want it at the top of the file then you can do this:

    Private Sub btnaddfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddfile.Click
        Dim mywright As IO.StreamWriter
        Dim item As String

        SaveFileDialog1.FileName = txtname.Text
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            mywright = New IO.StreamWriter(SaveFileDialog1.FileName, True)
            If mywright.BaseStream.Length = 0 Then
                mywright.WriteLine("lstHitLog.text")
            End If
            For Each item In lstHitLog.Items
                mywright.WriteLine(item)
            Next
            mywright.Close()
        End If
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial