|
Silverlight 3 – Open, Edit and Save a Text File to a Location outside the Isolated Storage
|
|
Rating: 3 user(s) have rated this article
Posted by: Suprotim Agarwal,
on 2/2/2010,
in category "Silverlight 2, 3 and 4"
Views: this article has been read 16290 times
Abstract: In this article, we will see how to Open, Edit and Save a Text File to a Location outside the Isolated Storage
Silverlight 3 – Open, Edit and Save a Text File to a Location outside the Isolated Storage
I see a lot of question on the forums asking how to edit text files kept on their machine and then save these files back to a desired location using Silverlight. In Silverlight 2, you had to use the Isolated Storage to save a file locally on the user’s machine. The SaveFileDialog is a new dialog control in Silverlight 3 which allows the user to specify a file name and location to save a file. This control can be invoked only through a user interaction. Let us a see a simple demonstration of using the OpenFileDialog and the SaveFileDialog control.
Create a simple user interface consisting of a TextBox and a Button. The user will first click on the Open button to open a local file and display the text in a TextBox control. After editing the text, the user will be presented the SaveFileDialog control where the user can specify the file name and location and persist the changes.
<UserControl x:Class="SilverlightSaveFileDialog.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Canvas>
<TextBox Name="txt" Canvas.Left="20" Canvas.Top="50" Width="150" Height="200"
AcceptsReturn="True"></TextBox>
<Button Name="btnOpen" Canvas.Left="20" Canvas.Top="270" Click="btnOpen_Click" Content="Open" ></Button>
<Button Name="btnSave" Canvas.Left="60" Canvas.Top="270" Click="btnSave_Click" Content="Save" ></Button>
</Canvas>
</UserControl>
Let us first code the btnOpen_click event to read data from a text file and then display it in a textbox.
C#
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text Files|*.txt";
ofd.FilterIndex = 1;
if (true == ofd.ShowDialog())
{
FileInfo info = ofd.File;
using (StreamReader sread = info.OpenText())
{
txt.Text = sread.ReadToEnd();
}
}
}
VB.NET
Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim ofd As New OpenFileDialog()
ofd.Filter = "Text Files|*.txt"
ofd.FilterIndex = 1
If True = ofd.ShowDialog() Then
Dim info As FileInfo = ofd.File
Using sread As StreamReader = info.OpenText()
txt.Text = sread.ReadToEnd()
End Using
End If
End Sub
When the user runs the application now and clicks on the Open button, an Open File dialog is presented to user that allows the user browse files on their system. The user can select a text file and click on Open. For demonstration purposes, let us open the Sample.txt file as shown below:
The ShowDialog() method returns true when ‘Open’ is clicked. The final step is to use the FileInfo.OpenText() to return a stream reader containing the text file contents.
The text gets displayed in the TextBox as shown below:
Once the user has edited the text in the textbox, we will persist the changes to Sample.txt. We will use the SaveFileDialog control for this purpose.
C#
private void btnSave_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = "Text Files (*.txt)|*.txt";
sfd.FilterIndex = 1;
if (true == sfd.ShowDialog())
{
using (Stream stream = sfd.OpenFile())
{
byte[] tbyte = (new UTF8Encoding(true)).GetBytes(txt.Text);
stream.Write(tbyte, 0, tbyte.Length);
stream.Close();
}
}
}
VB.NET
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim sfd As New SaveFileDialog()
sfd.DefaultExt = "txt"
sfd.Filter = "Text Files (*.txt)|*.txt"
sfd.FilterIndex = 1
If True = sfd.ShowDialog() Then
Using stream As Stream = sfd.OpenFile()
Dim tbyte() As Byte = (New UTF8Encoding(True)).GetBytes(txt.Text)
stream.Write(tbyte, 0, tbyte.Length)
stream.Close()
End Using
End If
End Sub
In the code shown above, we are saving the file contents using a stream by calling the OpenFile() method.
After the user clicks on the Save button, the SaveFileDialog control is displayed.
All you have to do is select the existing Sample.txt file and click on Save. You can also save the file to a different file location with a different name. If you decide to overwrite the existing file, the user is prompted with the following message:
Clicking on yes overwrites the contents of the file with the contents of the textbox.
You can read more about the SaveFileDialog control over here. The entire source code of this article can be downloaded over here
If you liked the article,
Subscribe to the RSS Feed or Subscribe Via Email.