Isolated Storage in Silverlight

I sometimes think that my doggy is jealous of me, because he thinks that I am more intelligent then him (well, sometimes I also doubt that, but I don't want to discuss this publically). I give him a meal twice a day, and he is very happy. There are times when he gets upset and does not eat. At that time he takes his food with him and hides it somewhere, though where is still a mystery. He takes out his food from that mysterious place when he is hungry and returns it back safely. If my doggy is Silverlight, then his place to hide food is Isolated Storage.
A Client-side storage functionality of Silverlight apart from cookies is Isolated Storage. One can store files, images, maybe even videos sometimes there. 

Caution: The size of Isolated Storage is limited, please do not try to store something in it that you shouldn't.

Let's create a simple Silverlight application to describe how to actually use Isolated Storage in various scenarios.
Create a Silverlight application IsolatedStorage. In the main page of it, we will have two buttons and a TextBox. One will set data in the TextBox in the Isolated Storage, and the other will retrieve that data and show it in a message box. Here is the XAML of the mane page for the previously mentioned design:

<UserControl x:Class="IsolatedStorage.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:DesignHeight="300" d:DesignWidth="400">

 

  <Grid x:Name="LayoutRoot" Background="White">

    <Button Content="Save data"

    HorizontalAlignment="Left"

    Margin="107,111,0,0" VerticalAlignment="Top"

    Width="75" Click="Button_Click_1"/>

    <Button Content="Get data"

    HorizontalAlignment="Left"

    Margin="200,111,0,0" VerticalAlignment="Top"

    Width="75" Click="Button_Click_2"/>

    <TextBox HorizontalAlignment="Left"

    x:Name="TxtBox" Height="23"

     Margin="132,65,0,0" TextWrapping="Wrap"

    Text="" VerticalAlignment="Top" Width="120"/>

  </Grid>

</UserControl>

In order to begin communicating with Isolated Storage, we need to create an object of IsolatedStorageFile class as "_file".

readonly IsolatedStorageFile _file = IsolatedStorageFile.GetUserStoreForApplication();

Now just like as we do with normal files, while handling local files in C#, we need to do the same here also. For instance, in order to write to a file with the name file.txt, we need to write the following line of code in the click event of the "Save Data" button.

 

private void Button_Click_1(object sender, RoutedEventArgs e)

{

     var fileContents = TxtBox.Text;

     var data = Encoding.UTF8.GetBytes(fileContents);

 

     using (var stream = _file.CreateFile("file.txt"))

     {

         stream.Write(data, 0, data.Length);

     }

}

Line #3 will get data from the TextBox. Line #4 will convert that data into a byte stream. Next a file in the Isolated Storage named file.txt is created, and from the byte stream, the data is written to the file. Very easy, isn't it?
To read the data back again, just do the complete reverse of the process. Here's the code for the click event of the "Get Data" button:

private void Button_Click_2(object sender, RoutedEventArgs e)

{

    using (var stream = new IsolatedStorageFileStream("file.txt", FileMode.Open, _file))

    {

        var length = stream.Length;

        var decoded = new byte[length];

        stream.Read(decoded, 0, (int)length);

        MessageBox.Show(Encoding.UTF8.GetString(decoded, 0, (int)length));

   }

}

 

Just the reverse, line #3 will open the file (saved previously) for reading. Line #7 will read the file stream, converting it into a byte stream. Line #8 will decode that byte stream back into a string, that will then be displayed via a message box on the screen.

Isolated Storage, though, may not seem very useful in Silverlight's web applications, but it is handy when working with Windows Phone. You can store user's information there, no need of a database, whatsoever. I will be discussing that in one of my future articles shortly. Until then, this is the short description of Isolated Storage.


Similar Articles