Question : Writing a formatted file to memory

I use the following function to open and read a formatted file to a RichTextBox and it works fine.  What I would like to do is to be able to save the file to memory, and then be able to call it later if the user wants to revert back to the original file.  (The user has the option to change the file inside of the text box).

So basically I want to be able to provide a button that lets the user revert back to the file stored in memory.  I know I need to use MemoryStream, but I just can't figure out how to make the write and read...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
Public Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        txtFile.Text = OpenFileDialog1.FileName.ToString()
        ' Try to close the open file
        OpenFileDialog1.Dispose()
        Dim ioOutput As String = ""
        ' txtFile.Text if the file name opened earlier
        If File.Exists(txtFile.Text) Then
            Dim ioFile As New StreamReader(txtFile.Text)
            Dim ioLine As String = ""
            Dim ioLines As String = ""
            While Not ioFile.EndOfStream
                ioLine = ioFile.ReadLine
                ioLines = ioLines & vbCrLf & ioLine
            End While
            ioOutput = ioLines

            'Lets store the file to memory just in case
                   ' Place code that stores the ioOutput to memory here

            ' Try to close the open file again
            ioFile.Close()
        End If
        ' Place the result Stream in a RichTextBox
        RichTextBox1.Text = ioOutput

    End Sub

Answer : Writing a formatted file to memory

Just store it in a string at the class level...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
Public Class Form1

    Private FileData As String = ""

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using ofd As New OpenFileDialog
            If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Try
                    FileData = My.Computer.FileSystem.ReadAllText(ofd.FileName)
                    txtFile.Text = ofd.FileName
                    RichTextBox1.Text = FileData
                Catch ex As Exception
                    MessageBox.Show("File: " & ofd.FileName & vbCrLf & vbCrLf & ex.ToString, "Error Loadin File", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End Using
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RichTextBox1.Text = FileData
    End Sub

End Class
Random Solutions  
 
programming4us programming4us