Isolated Storage in Silver Light

Why Isolated Storage?

SilverLight applications are partially trusted application, which are running on 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 client computer, this may harm many important system files. Because we know some SilverLight application might be potential malicious application. And we don’t want while browsing, any harmful SilverLight application to access our file system and harm them.But some time, SilverLight application needs some restricted access to client local file system for business requirement. Like Reading and Writing user specific file at client location or saving the personal information specific to user at client side etc. So, above purpose is achieved by SilverLight application using ISOLATED STORAGE.


Cookies and Isolated Storage 

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 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\Silver light\is
   

 SiliverLight application has access to two different stores.


SILVERLIGHT application has access of

  1. User + Application store
  2. User + Site store

Few Important points

  1. A SilverLight Application can access only its own application store. It cannot access store of other 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 full URL of the XAP file of the Silver light Application. For example www.c-sharpcorner.com\Application1.xap
  4. Application identity is case insensitive.


 Sample:
This sample will demonstrate how to use with Application level setting of IsolatedStorageSetting class.

  1. After entering User Name is textbox , when user will click Save Me. That user name will get added in the isolated file created for the application.
  2. On clicking on Reset Me button value saved against UserName key, will get deleted from the isolated file.
  3. At each time application will loaded, saved UserName will get display in the textbox

Step 1

Create a private global variable of the type IsolatedStorageSetting for the Application level.
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.
  3. App.add() is used to add new key in the application.
private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e){

if (chkSave.IsChecked == true)
{
if (!(app.Contains(“UserName”)))
{
app.Add(“UserName”,“User Name has not Set “);
}
app[“UserName”] = txtUname.Text;
} }

 Step 3
On the Reset button, we are simple removing the key from the app setting.

private void btnReset_Click(object sender, System.Windows.RoutedEventArgs e)

{

app.Remove(“UserName”);

txtUname.Text = “”;}

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

void MainPage_Loaded(object sender, RoutedEventArgs e){

if (app.Contains(“UserName”))
{
txtUname.Text = app[“UserName”].ToString();
}}

 So, the complete code is as below,
MainPage.Xaml

<UserControl x:Class=”IsolatedStorageSample1.MainPage”
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

Width=”Auto” Height=”Auto”>

<Grid x:Name=”LayoutRoot” Background=”#FF7F4343″ Width=”400″ Height=”400″>

<Grid.ColumnDefinitions>    
 <ColumnDefinition Width=”0.385*”/>
<ColumnDefinition Width=”0.615*”/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>

<RowDefinition Height=”0.25*”/>
<RowDefinition Height=”0.157*”/>
<RowDefinition Height=”0.593*”/>
 </Grid.RowDefinitions>
<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″/>
 <TextBlock Margin=”8,8,8,8″ Text=”UserName” TextWrapping=”Wrap” FontSize=”12″ FontWeight=”Bold”Foreground=”#FF342A2A” FontFamily=”Arial Rounded MT Bold”/>
<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”/>
<Grid Margin=”8,10,12,8″ Grid.Column=”1″ Grid.Row=”1″>

 <Grid.ColumnDefinitions>

<ColumnDefinition Width=”0.385*”/>
<ColumnDefinition Width=”0.615*”/>
 </Grid.ColumnDefinitions>

<Button x:Name=”btnSave” Margin=”2,2,2,2″ Content=”Save Me” Grid.Row=”0″ Grid.Column=”0″ Click=”btnSave_Click”/>

<Button x:Name=”btnReset” Margin=”2,2,2,2″ Content=”Reset Me “ Grid.Row=”0″ Grid.Column=”1″ Click=”btnReset_Click”/>
 </Grid>
</Grid>
</UserControl>

 MainPage.Xaml.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.IO.IsolatedStorage;

 namespace IsolatedStorageSample1{
public partial class MainPage : UserControl{
private IsolatedStorageSettings app = IsolatedStorageSettings.ApplicationSettings;
public MainPage(){

InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e){
if (app.Contains(“UserName”)){
txtUname.Text = app[“UserName”].ToString();
}}
private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e)

{
if (chkSave.IsChecked == true){
if (!(app.Contains(“UserName”)))
{
app.Add(“UserName”,“User Name has not Set “);
}
app[“UserName”] = txtUname.Text;}}
private void btnReset_Click(object sender, System.Windows.RoutedEventArgs e){
app.Remove(“UserName”);

txtUname.Text = “”;}}}

 Output:


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


Quota in Isolated Storage

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

  1. An Application can request more space for the quota group

      

  2.    Sample on how to modify the quota?
    I have added the below code on the click event of Save Button. So now the save Button click event is as below. I have modified the above code sample. See the code in Red rectangle.
private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e){

if (chkSave.IsChecked == true){
if (!(app.Contains(“UserName”)))
{
app.Add(“UserName”,“User Name has not Set “);
}
app[“UserName”] = txtUname.Text;
}
using (var store = IsolatedStorageFile.GetUserStoreForApplication()){
int spaceneed = 1024 * 1024 * 5;
if (store.AvailableFreeSpace < spaceneed){
if (store.IncreaseQuotaTo(store.Quota + spaceneed)){
MessageBox.Show(“User Has approved the quota Increase”);
MessageBox.Show(store.AvailableFreeSpace.ToString());
}
else{
MessageBox.Show(“User Has Not Approved the quota Increase “);
MessageBox.Show(store.AvailableFreeSpace.ToString());
}} } }

 Output
User is getting prompt for Approval.


 When User is not approving means selecting “No”. The default size is getting displayed in message box.


When User is approving means selecting “Yes”. The increased size is getting displayed in message box.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com