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.FileN ame, FileMode.Append, IO.FileAccess.Write)
mywright = New IO.StreamWriter(mystream)
mywright.WriteLine("lstHit Log.text")
For inti = 0 To lstHitLog.Items.Count - 1
lstHitLog.SelectedIndex = inti
mywright.Write(lstHitLog.T ext)
Next
mywright.Close()
End Sub
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.FileN
mywright = New IO.StreamWriter(mystream)
mywright.WriteLine("lstHit
For inti = 0 To lstHitLog.Items.Count - 1
lstHitLog.SelectedIndex = inti
mywright.Write(lstHitLog.T
Next
mywright.Close()
End Sub
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.Fil eName, True)
mywright.WriteLine("lstHit Log.text")
For Each item In lstHitLog.Items
mywright.WriteLine(item)
Next
mywright.Close()
End If
End Sub
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.Fil
mywright.WriteLine("lstHit
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
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:\header s.txt", True)
For Each x As Object In ListBox1.Items
mywright.WriteLine(x.ToStr ing)
Next
mywright.Close()
End Sub
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:\header
For Each x As Object In ListBox1.Items
mywright.WriteLine(x.ToStr
Next
mywright.Close()
End Sub
Way to late :-)
Good morning TheLearnedOne and Idle_Mind :-)
Good afternoon, Ronald =:)
Bob
Bob
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.
ASKER
I had a line i did not need "mywright.WriteLine("lstHi tLog.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
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?
Do you want "lstHitLog.text" only at the top of the file...or not in the file at all?
ASKER
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(SaveFileDi alog1.File Name, True)
If mywright.BaseStream.Length = 0 Then
mywright.WriteLine("lstHit Log.text")
End If
For Each item In lstHitLog.Items
mywright.WriteLine(item)
Next
mywright.Close()
End If
End Sub
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
mywright = New IO.StreamWriter(SaveFileDi
If mywright.BaseStream.Length
mywright.WriteLine("lstHit
End If
For Each item In lstHitLog.Items
mywright.WriteLine(item)
Next
mywright.Close()
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Easy: just replace mywright.Write(.. with mywright.WriteLine(... - like you did just above the for-loop.
John