WPF Load and Save a Text File in VB.NET

This article defines a scenario where you have a text file and you want to enhance its formatting. you can load it, edit it and after that you can save it.
  • 5136
 

This article defines a scenario where you have a text file and you want to enhance its formatting. you can load it, edit it and after that you can save it. you can load and save content with a WPF RichTextBox.

Loading a Text file

To load a text file we need to get text file to the RichTextBox. So we take two Button and a RichTextBox control on the WPF window. 

r1.gif

Figure 1.

XAML code

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">

    <Grid>

          <Grid.RowDefinitions>

            <RowDefinition Height="68*" />

            <RowDefinition Height="224*" />

 

        </Grid.RowDefinitions>

        <RichTextBox Margin="43,0,49,86" Name="RichTextBox1" Background="#FFEDEAEA" Grid.RowSpan="2">

 

        </RichTextBox>

        <Button Content="Load Text" Height="23" HorizontalAlignment="Left" Margin="64,183,0,0" Name="Button1" VerticalAlignment="Top" Width="75" Grid.Row="1" />

        <Button Content="Save text" Height="23" HorizontalAlignment="Right" Margin="0,183,184,0" Name="Button2" VerticalAlignment="Top" Width="75" Grid.Row="1"/>

    </Grid>

 </Window>

 

Now we makes a text file which has the name roh.txt in the F folder.

 

Dim TextFile As String = "F:\roh.txt"

Now double click on the button control and add the following code.

Dim TextFile As String = "F:\roh.txt"

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        Dim newContent As String = IO.File.OpenText(TextFile).ReadToEnd()

        RichTextBox1.Document.Blocks.Add(New Paragraph(New Run(newContent)))

    End Sub

Saving a text File

After the loading a file we want some change in the file such as edit it and save it with change.

Now double click on the save button and add the following code with the button Save text.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click

        Dim fs As IO.FileStream = IO.File.OpenWrite(TextFile)

        Dim RTBText As New TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd)

        RTBText.Save(fs, DataFormats.Text)

        fs.Close()

        MessageBox.Show(" Text file has saved after edit")

    End Sub

Now run the application and test it.

r2.gif

Figure 2.

Now click on the load Button to load a text file.

r3.gif

Figure 3.

Now we can enhance formatting to add some text or delete.

r4.gif

Figure 4.

And now click on the save text button. This will save the file with the change its proper position.

r5.gif

Figure 5.

Now open the file and test it.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.