articles

Read and Write Text files in Silverlight

Anonymous User23206 07-Mar-2011

In this demonstration am going to tell you that how to read and write text files in Silverlight. 

A XMAL code which represents user interface for this application
<Grid x:Name="LayoutRoot" Background="White" Width="450" Height="300" Loaded="LayoutRoot_Loaded">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="130"/>
        </Grid.ColumnDefinitions>
        <TextBox x:Name="txtContent" Grid.Row="0" Grid.Column="0" Width="300" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" />
        <Button Content="Open File" Name="button1" Grid.Column="1" Height="30" VerticalAlignment="Center" Margin="0,104,0,166" Click="button1_Click" />
        <Button Content="Create File" Name="button2" Grid.Column="1" Height="30" VerticalAlignment="Center" Margin="0,140,0,130" Click="button2_Click" />
        <Button Content="Save File" Name="button3" Grid.Column="1" Height="30" VerticalAlignment="Center" Margin="0,176,0,94" Click="button3_Click" />
</Grid>
Output of the above code snippet is as follows

Read and Write Text files in Silverlight

When user click on Open File button then an Open File Dialog box is open through which user can select any text file which he needs to be open. When user click Create File button then an Open file button is disable and Save File button is enable. After writing content in text file when user click on Save File button then all the records is saved by using save file dialog box.

Note – For implementing concept of OpenFileDialog and SabeFileDialog concept please import Microsoft.Win32 namespace.

Write Following code at the click event of Open File button
private void button1_Click(object sender, RoutedEventArgs e)
{
            txtContent.Text = "";
            button2.IsEnabled = false;
            button3.IsEnabled = false;
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog().Value)
            {
                using (StreamReader sr = new StreamReader(ofd.File.OpenRead()))
                {
                    txtContent.Text = sr.ReadToEnd();
                }
            }
            button2.IsEnabled = true;
            button3.IsEnabled = true;
}
Write following code at the click event of Create File Button
private void button2_Click(object sender, RoutedEventArgs e)
{
            txtContent.Text = "";
            button2.IsEnabled = false;
            button1.IsEnabled = false;
            button3.IsEnabled = true;
}
Write following code at the click event of Save File button
private void button3_Click(object sender, RoutedEventArgs e)
{
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog().Value)
            {
                using (StreamWriter sw = new StreamWriter(sfd.OpenFile()))
                {
                    sw.Write(txtContent.Text);
                    sw.AutoFlush = true;
                    sw.Flush();
                }
            }
            button1.IsEnabled = true;
            button2.IsEnabled = true;
}

 

At the load event of grid control disable the Dave File button.

Output of the following code is as follows

Read and Write Text files in Silverlight

Read and Write Text files in Silverlight

These two figures are used for opening file. Next to figures are used for saving content in File.

Read and Write Text files in Silverlight

Read and Write Text files in Silverlight

Summary

In this demonstration we learn how to read and write text files in Silverlight.

 


Updated 10-Apr-2020
I am a content writter !

Leave Comment

Comments

Liked By