简体   繁体   中英

How to save/load data from RichTextBox to/from file

I have Richtextbox to input text with multiple font names, color, backcolor and size.

I want to save it and load it with the same setting.

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
    SaveFileDialog1.Filter = "TextFile (*.txt;*.rtf)|*.txt;*.rtf|Batch File (*.bat)|*.bat|All Files (*.*)|*.*"
    SaveFileDialog1.FileName = "Untitled"
    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim a() As String = SaveFileDialog1.FileName.Split("\")
        Dim sw As New StreamWriter(SaveFileDialog1.FileName)
        sw.Write(RichTextBox1.Text)
        My.Settings.Save()
        sw.Close()
    End If
End Sub

You can use this code for reading:

RichTextbox1.text = System.IO.File.ReadAllText(openfilepath)

And this for writing:

System.IO.File.WriteAllText(Savefiledialog1.FileName, Richtextbox1.text)

Hope it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM