• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

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
0
bassendoughboy
Asked:
bassendoughboy
  • 6
  • 3
  • 3
  • +2
1 Solution
 
softplusCommented:
Hi bassendoughboy,
Easy: just replace mywright.Write(.. with mywright.WriteLine(...  - like you did just above the for-loop.
John
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Bob LearnedCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RonaldBiemansCommented:
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
0
 
RonaldBiemansCommented:
Way to late :-)
0
 
RonaldBiemansCommented:
Good morning TheLearnedOne and Idle_Mind :-)
0
 
Bob LearnedCommented:
Good afternoon, Ronald =:)

Bob
0
 
bassendoughboyAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I need coffee and the kids have the day off from school...but good morning to all as well.    ;)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
When you open in Append mode as in my example it will create or append to the file as necessary.
0
 
bassendoughboyAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
 
bassendoughboyAuthor Commented:
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?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Set the OverwritePrompt() property of your SaveFileDialog to False:

    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.OverwritePrompt = False
        SaveFileDialog1.FileName = txtname.Text
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            mywright = New IO.StreamWriter(SaveFileDialog1.FileName, True)

            ' comment this block out if you don't want "lstHitLog.text" at the top
            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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now