I think sometimes, that my doggy is jealous of me, because he thinks that I am more intelligent then him (well, sometimes I also doubt that, whatever I don’t want to discuss this publically). I give him meal twice a day, and he is very happy. There are times when he gets upset and do not eat. At that time he take his food with him and hide it somewhere, though that somewhere is still a mystery. He take out his food from that mysterious place when he is hungry and keep it back safe then. If my doggy is Silverlight, then his place of hiding food is Isolated Storage.
Client side storage functionality of Silverlight apart from cookies is Isolated Storage. One can store files, images, maybe videos sometimes in this storage.
Caution: Size of Isolated storage is limited, please do not try to store something in it which 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 mainpage of it, we will have two buttons and a textbox. One will set data in the textbox in the isolated storage, and other will retrieve that data and show it in a message box. Here is the xaml of the mane page for the above mentioned design.
1: <UserControl x:Class="IsolatedStorage.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400">
8:
9: <Grid x:Name="LayoutRoot" Background="White">
10: <Button Content="Save data"
11: HorizontalAlignment="Left"
12: Margin="107,111,0,0" VerticalAlignment="Top"
13: Width="75" Click="Button_Click_1"/>
14: <Button Content="Get data"
15: HorizontalAlignment="Left"
16: Margin="200,111,0,0" VerticalAlignment="Top"
17: Width="75" Click="Button_Click_2"/>
18: <TextBox HorizontalAlignment="Left"
19: x:Name="TxtBox" Height="23"
20: Margin="132,65,0,0" TextWrapping="Wrap"
21: Text="" VerticalAlignment="Top" Width="120"/>
22: </Grid>
23: </UserControl>
In order to begin communicating with isolated storage, we need to create an object of IsolatedStorageFile class as _file.
1: readonly IsolatedStorageFile _file = IsolatedStorageFile.GetUserStoreForApplication();
Now just like as we do with normal files, while handling local files in c#, same we need to de here also. For instance, in order to write file with the name file.txt, we need to write following line of code in the click event of the Save Data button.
1: private void Button_Click_1(object sender, RoutedEventArgs e)
2: {
3: var fileContents = TxtBox.Text;
4: var data = Encoding.UTF8.GetBytes(fileContents);
5:
6: using (var stream = _file.CreateFile("file.txt"))
7: {
8: stream.Write(data, 0, data.Length);
9: }
10: }
Line #3 will get data from the textbox. Line #4 will convert that data into byte stream. Next a file in the Isolated Storage named file.txt is created, and from byte stream, the data is written in the file. Very easy, isn’t it?
To read the data back again, just a complete reverse of the process is to be followed. Here’s the code for the click event of the Get Data button.
1: private void Button_Click_2(object sender, RoutedEventArgs e)
2: {
3: using (var stream = new IsolatedStorageFileStream("file.txt", FileMode.Open, _file))
4: {
5: var length = stream.Length;
6: var decoded = new byte[length];
7: stream.Read(decoded, 0, (int)length);
8: MessageBox.Show(Encoding.UTF8.GetString(decoded, 0, (int)length));
9: }
10: }
Just reverse, line #3 will open the file (saved previously) for reading. Line #7 will read the file stream converting it into byte stream. Line #8 will decode that byte stream back into string, which will then be displayed via message box on the screen.
Isolated storage though may not seem very useful in Silverlight's web applications, but it do come in handy while working with Windows Phone. You can store user’s information their, no need of database, whatsoever. I will be discussing about that in one of my next post in time shortly. Till then, this is the short description on Isolated Storage.
0 comments