.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Isolated Storage in Silverlight Tutorial

Posted By:Dhananjay Kumar       Posted Date: December 27, 2009    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  
 

Isolated Storage in Silverlight Tutorial


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

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

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 malicious Silverlight application can not have access to the original file system.

Location of the Isolated Storage

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


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 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 Silverlight Application. For example http:\\www.dotnetspark.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 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.


7.gif 

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. 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;
      }
      }
  3. App.add() is used to add new key in the application.

    8.gif



8.gif

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:

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 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 Silverlight, 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.

    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 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. 

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.

13.gif


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

14.gif


When User is approving means selecting "Yes". The increased size is getting displayed in 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!


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend