hiya,

What i am trying to do is save text from a number of text boxes to a user named file then subsequently be able to reload the data back into the same text boxes at a later date.

so far i have this which was provided for elsewhere on the web, this allows me to save comma delineated data to a file which is fine but i can't seem to reload the data. As an experiment i have just tried to reload it back into a rich text box to see if the process would work but nothing as yet.

Private Sub SAveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SAveToolStripMenuItem.Click
        Dim savedfile As String
        savedfile = TextBoxinc1.Text & "," & TextBoxinc2.Text & "," 
        Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter
        Save.Filter = "Text Format [*.txt*]|*.txt|All Files [*.*]|*.*"

        Save.CheckPathExists = True
        Save.Title = "Save"
        Save.ShowDialog(Me)
        Try
        Catch ex As Exception
        End Try

        myStreamWriter = System.IO.File.AppendText(Save.FileName)
        myStreamWriter.Write(savedfile)
        myStreamWriter.Flush()
        Try
        Catch ex As Exception
        End Try

    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim Open As New OpenFileDialog()
        Dim myStreamReader As System.IO.StreamReader
        Open.Filter = "Text Format [*.txt*]|*.txt|All Files [*.*]|*.*"
        Open.CheckFileExists = True
        Open.Title = "Open"
        Open.ShowDialog(Me)
        Try
            Open.OpenFile()
            myStreamReader = System.IO.File.OpenText(Open.FileName)
            RichTextBox1.Rtf = myStreamReader.ReadToEnd()
        Catch ex As Exception
        End Try
    End Sub
End Class

Hi!

Change line 34 like this:

RichTextBox1.Text = myStreamReader.ReadToEnd()

You need to use "Text" property instead of "Rtf".

You can also do it all in one line of code. Keeps fingers happy. :D

RichTextBox1.Text = IO.File.ReadAllText(Open.FileName)

Thanks that fixed it, what i was also after is at the moment when saving the file it simply appends the data to the end of the file, what i would like is if the user selects to save a file under a name that already exists they can and that would entail wiping the old file and creating a new file with the same name and with the new data.

myStreamWriter = System.IO.File.AppendText(Save.FileName)
myStreamWriter.Write(savedfile)
myStreamWriter.Flush()

what would be the correct syntax to do this?

See if this helps.

Dim sfd As New SaveFileDialog
        With sfd
            .Title = "Save"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                If IO.File.Exists(.FileName) Then
                    If MsgBox("Append Text to File?" & vbNewLine & "Clicking No will overwrite the current file.", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                        '// append code here.
                    Else
                        '// overwrite code here.
                    End If
                Else '// if file does not exist.
                    '// overwrite code here.
                End If
            End If
        End With
This article has been dead for over six months. Start a new discussion instead.