Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Streamwriter writes to text file..start writing at end of file

Status
Not open for further replies.
Aug 1, 2003
39
US
Hi

I have information from textboxes that are being placed into a textfile from a streamwriter but I want it to write to the end of the file, like add on to what is already there. How can I do that?

Code:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim filewriter As StreamWriter
        Dim output As FileStream

        Dim filechooser As New SaveFileDialog
        Dim result As DialogResult = filechooser.ShowDialog
        Dim filename As String

        If result = Windows.Forms.DialogResult.Cancel Then
            Return
        End If

        filename = filechooser.FileName
        output = New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)
        filewriter = New StreamWriter(output)
        'btnReadFile.Enabled = True


        filewriter.WriteLine(txtFirst.Text & vbTab & txtLast.Text & vbTab & _
        txtID.Text & vbTab & txtGrade.Text & vbTab & txtClass.Text)
        filewriter.Close()

        txtFirst.Text = ""
        txtLast.Text = ""
        txtID.Text = ""
        txtGrade.Text = ""
        txtClass.Text = ""


    End Sub


--------------------------
Thanks
Brian

 
Create the StreamWriter object like this:

filewriter = New StreamWriter(output, [blue]True[/blue])

The 'True' in the second parameter tells the StreamWriter to append to the file. 'False' (the default) will overwrite the file.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
This gives the syntax error...

Overload resolution failed because no accessible 'New' can be called with these arguments:

'Public Sub New(path As String, append As Boolean)': Value of type 'System.IO.FileStream' cannot be converted to 'String'.




--------------------------
Thanks
Brian

 
Actually I think it is this line:
output = New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)

I believe you should change that to:
output = New FileStream(filename, FileMode.Append, FileAccess.Write)

Or you could change the code from so you don't create a FileStream object, and instead just create a StreamWriter object with the path as the first parameter and a boolean True as the second parameter (As true for append).

Hope that helps.
 
Thanks (FileMode.Append) did the trick...

Code:
output = New FileStream(filename, FileMode.Append, FileAccess.Write)

--------------------------
Thanks
Brian

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top