Introduction to Isolated Storage in Silverlight

This article provides a theoretical introduction to Isolated Storage in Silverlight. This will also show two samples. One for ISolatedStorageSetting and another to increase quota.

This is a technique in Silverlight to create a Virtual File System in the client machine where a Silverlight application is running. This is used to have direct restricted access to a client operating system file system.

Why Isolated Storage?

Silverlight applications are partially trusted applications, which are running in a sandbox environment. So, Silverlight applications are not allowed to access the file system of the client where it is running, for the security reason. Because it is not safe to allow any Silverlight application to access a client computer, this may harm many important system files. Because we know that a Silverlight application might be a potential malicious application. And while browsing we don't want any harmful Silverlight application to access our file system and harm them.

But sometimes a Silverlight application needs some restricted access to the client local file system for business requirements. Like Reading and Writing user specific file at client location or saving the personal information specific to user at client side etc.

So the above purpose is achieved by Silverlight application using ISOLATED STORAGE.

Cookies and Isolated Storage

1.gif

Isolation

Isolated storage is composed of many different unique stores, each of which can be thought of as its own virtual file system. This virtual file system is in isolation with the file system of operating system, such that a malicious Silverlight application can not have access to the original file system.

Location of the Isolated Storage

Operating System Location
Windows VISTA <SYSDRIVE>\Users\<user>\AppData\LocalLow\Microsoft\Silverlight\is
Windows XP <SYSDRIVE>\Document and Settings\<user>\Local Setting\Application data\Microsoft\Silverlight\is

A Silverlight application has access to two different stores.

2.gif

3.gif

4.gif

5.gif

Few Important points
  1. A Silverlight Application can access only its own application store. It cannot access the store of another application.
  2. The Application Store is based on the, identity of the user and identity of the application.
  3. The identity of the application is the full URL of the XAP file of the Silverlight Application. For example www.c-sharpcorner.com\Application1.xap
  4. Application identity is case insensitive.

6.gif 

Sample Objective

This sample will demonstrate how to use with Application level setting of IsolatedStorageSetting class.
  1. After entering User Name in the textbox, then the user will click Save Me. That user name will get added in the isolated file created for the application.
  2. On clicking the Reset Me button, the value saved for the UserName key, will get deleted from the isolated file.
  3. Each time the application loads, the saved UserName will be displayed in the textbox.
7.gif 

Step 1

Create a private global variable of the type IsolatedStorageSetting for the Application level.
  1. private IsolatedStorageSettings app = IsolatedStorageSettings.ApplicationSettings;
Step 2

On the Save button click, we will be checking if checkbox is checked: we will check whether: 
  1. UserName key is existing in App or not. If not, we will add UserName key.
  2. We will set value for UserName key from the user name text box.
    1. private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e)  
    2. {  
    3.     if (chkSave.IsChecked == true)  
    4.     {  
    5.         if (!(app.Contains("UserName")))  
    6.         {  
    7.             app.Add("UserName","User Name has not Set ");  
    8.         }  
    9.         app["UserName"] = txtUname.Text;   
    10.     }  
    11. }

  3. App.add() is used to add new key in the application.

    8.gif
Step 3

On the Reset button, we are simply removing the key from the app setting.

  1. private void btnReset_Click(object sender, System.Windows.RoutedEventArgs e)  
  2. {  
  3.     app.Remove("UserName");  
  4.     txtUname.Text = "";  
  5. }

Step 4

On the Page Load, I am displaying the saved value for the key UserName from the isolated storage.

  1. void MainPage_Loaded(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (app.Contains("UserName"))  
  4.     {  
  5.         txtUname.Text = app["UserName"].ToString();  
  6.     }  
  7. }   
So, the complete code is as below:

MainPage.Xaml

  1. <UserControl x:Class="IsolatedStorageSample1.MainPage"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.     Width="Auto" Height="Auto">  
  5.    <Grid x:Name="LayoutRoot" Background="#FF7F4343" Width="400" Height="400">  
  6.          <Grid.ColumnDefinitions>  
  7.                   <ColumnDefinition Width="0.385*"/>  
  8.                   <ColumnDefinition Width="0.615*"/>  
  9.          </Grid.ColumnDefinitions>  
  10.          <Grid.RowDefinitions>  
  11.                   <RowDefinition Height="0.25*"/>  
  12.                   <RowDefinition Height="0.157*"/>  
  13.                   <RowDefinition Height="0.593*"/>  
  14.          </Grid.RowDefinitions>  
  15.          <TextBox x:Name="txtUname" Margin="10,10,10,10" Grid.Column="1" Text="" TextWrapping="Wrap" Background="#FF230303" BorderThickness="2,2,2,2" FontSize="16" FontWeight="Bold" Foreground="#FFF81616"/>  
  16.          <TextBlock  Margin="8,8,8,8" Text="UserName" TextWrapping="Wrap" FontSize="12" FontWeight="Bold" Foreground="#FF342A2A" FontFamily="Arial Rounded MT Bold"/>  
  17.          <CheckBox x:Name="chkSave" Margin="10,10,10,10" Grid.Row="1" Content="Save User Name" IsChecked="True" FontWeight="Bold" FontSize="12"  BorderThickness="2,2,2,2" Foreground="#FF150808" FontFamily="Arial Rounded MT Bold"/>  
  18.          <Grid Margin="8,10,12,8" Grid.Column="1" Grid.Row="1">  
  19.                     <Grid.ColumnDefinitions>     
  20.                   <ColumnDefinition Width="0.385*"/>  
  21.                   <ColumnDefinition Width="0.615*"/>  
  22.          </Grid.ColumnDefinitions>  
  23.                   <Button x:Name="btnSave" Margin="2,2,2,2" Content="Save Me" Grid.Row="0" Grid.Column="0" Click="btnSave_Click"/>  
  24.                       <Button x:Name="btnReset" Margin="2,2,2,2" Content="Reset Me " Grid.Row="0" Grid.Column="1" Click="btnReset_Click"/>  
  25.                   </Grid>    </Grid></UserControl>   
MainPage.Xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using System.IO.IsolatedStorage;   
  13.   
  14. namespace IsolatedStorageSample1  
  15. {   
  16.   
  17.     public partial class MainPage : UserControl  
  18.     {  
  19.         private IsolatedStorageSettings app = IsolatedStorageSettings.ApplicationSettings;    
  20.   
  21.         public MainPage()  
  22.         {  
  23.             InitializeComponent();  
  24.             Loaded += new RoutedEventHandler(MainPage_Loaded);  
  25.         }   
  26.   
  27.         void MainPage_Loaded(object sender, RoutedEventArgs e)  
  28.         {         
  29.   
  30.             if (app.Contains("UserName"))  
  31.             {  
  32.                 txtUname.Text = app["UserName"].ToString();  
  33.             }  
  34.         }   
  35.   
  36.         private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e)  
  37.         {  
  38.             if (chkSave.IsChecked == true)  
  39.             {  
  40.                 if (!(app.Contains("UserName")))  
  41.                 {  
  42.                     app.Add("UserName","User Name has not Set ");  
  43.                 }  
  44.                app["UserName"] = txtUname.Text;   
  45.             }  
  46.         }  
  47.   
  48.         private void btnReset_Click(object sender, System.Windows.RoutedEventArgs e)  
  49.         {  
  50.             app.Remove("UserName");  
  51.             txtUname.Text = "";  
  52.         }  
  53.     }  
  54. }  
Output

9.gif

After clicking Refresh again we will get the same output. If we click Reset and then press F5, we will get output as below.

10.gif

Quota in Isolated Storage

  1. A quota is a limit on the amount of isolated storage space that can be used by the application.
  2. A Quota Group is a group of isolated Stores that shares the same quota.
  3. In Silverlight, stores are grouped by the site. So all the application with same site identity shares the same quota.
  4. Isolated Storage can have zero or more quota groups.

    11.gif
  5. An Application can request more space for the quota group.

    12.gif
Sample on how to modify the quota?

I have added the following code on the click event of the Save Button. So now the save Button click event is as below. I have modified the above code sample.
  1. private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e)  
  2. {  
  3.     if (chkSave.IsChecked == true)  
  4.     {  
  5.         if (!(app.Contains("UserName")))  
  6.         {  
  7.             app.Add("UserName""User Name has not Set ");  
  8.         }  
  9.         app["UserName"] = txtUname.Text;  
  10.     }  
  11.   
  12.     using (var store = IsolatedStorageFile.GetUserStoreForApplication())  
  13.     {  
  14.         int spaceneed = 1024 * 1024 * 5;  
  15.         if (store.AvailableFreeSpace < spaceneed)  
  16.         {  
  17.             if (store.IncreaseQuotaTo(store.Quota + spaceneed))  
  18.             {  
  19.                 MessageBox.Show("User Has approved the quota Increase");  
  20.                 MessageBox.Show(store.AvailableFreeSpace.ToString());  
  21.             }  
  22.             else  
  23.             {  
  24.                 MessageBox.Show("User Has Not Approved the quota Increase ");  
  25.                 MessageBox.Show(store.AvailableFreeSpace.ToString());  
  26.             }  
  27.         }  
  28.     }  
  29. }  
Output

User is getting prompt for Approval.

13.gif


When the user is not approved means selecting "No". The default size is getting displayed in the message box.

14.gif


When the user is approved means selecting "Yes". The increased size is getting displayed in the message box.

15.gif

Conclusion

This article gave a basic introduction of Isolated Storage in Silverlight. It gave a theoretical introduction as well as two samples. Please find the attached source code for your reference.

Thank you for Reading. Happy Coding!