Silverlight Tip of the Day #20 – How to Increase your Isolated Storage Quota.

Each application by default is given 1 MB of storage space through Isolated Storage where the server is able to store client specific data on the clients machine. So what if you need more than 1 MB? Fortunately, the IsolatedStorageFile object provides a method called IncreaseQuotaTo() that allows a server to prompt the user for permission to increase the amount of storage.

You can see the list of applications that are using IsolatedStorage on your box. To do this:

  1. Right click on any Silverlight Application and from the context menu choose the only option: “Silverlight Configuration”.

SC

  1. This will bring up the Silverlight Configuration Dialog: 

    image

  2. Click on the last tab “Application Storage” and you will see a list of all web sites that are using Isolated Storage on this machine. The current amount of space each app is using as well as the quota allowed per app are also listed. 

    image

For our demo we will be displaying 3 pieces of information:

  1. Current Spaced Used
  2. Current Space Available
  3. Current Quota

I have added a text box where you can specify the number of bytes you wish to increase your quota by. By clicking on the button, this will then make the necessary call to IncreaseQuotaTo() in order to increase the quota.

***Note: You must call this function from a user event such as a button click. For security reasons, calling IncreaseQuotaTo() directly will automatically return false since the user did not instantiate the call.

Below is a screen shot of the test application we are creating:

3

When you enter a value and click on the button you will be prompted with this dialog:

4

If you click yes, you will see the new results:

image

Now, let’s look at our XAML code in Page.xaml that we use to set up this interface:

 <UserControl x:Class="IncreaseIsolatedStorage.Page"
     xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
     Width="800" Height="600">
 <Grid x:Name="LayoutRoot" Background="White">
     <Canvas Canvas.Left="10" Canvas.Top="10">
         <TextBlock Canvas.Left="10" x:Name="SpacedUsed" >Current Spaced Used=</TextBlock>
         <TextBlock Canvas.Left="10" x:Name="SpaceAvaiable" Canvas.Top="20">Current Space Available=</TextBlock>
         <TextBlock Canvas.Left="10" x:Name="CurrentQuota" Canvas.Top="40">Current Quota=</TextBlock>
         <TextBlock Canvas.Left="10" x:Name="NewSpace" Canvas.Top="70">New space (in bytes) to request=</TextBlock>
         <TextBox Canvas.Left="255" Canvas.Top="70" Width="100" x:Name="SpaceRequest"></TextBox>
         <TextBlock Canvas.Left="365" Canvas.Top="70" Width="60">(1048576 = 1 MB)</TextBlock>
         <Button Canvas.Left="10" Content="Increase Storage" Canvas.Top="100" 
                 Width="100" Height="50" Click="Button_Click">                
         </Button>
         <TextBlock Canvas.Left="10" Canvas.Top="160" x:Name="Results"></TextBlock>
     </Canvas>
 </Grid>
 </UserControl>

And our code behind:

 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 SilverlightApplication11
 {
     public partial class Page : UserControl
     {
         public Page()
         {
             InitializeComponent();
  
             SetStorageData();
         }
  
         private void SetStorageData()
         {
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 SpacedUsed.Text     = "Current Spaced Used = "+(isf.Quota - isf.AvailableFreeSpace).ToString() +" bytes";
                 SpaceAvaiable.Text  = "Current Space Available=" + isf.AvailableFreeSpace.ToString() + " bytes";
                 CurrentQuota.Text   = "Current Quota=" + isf.Quota.ToString() + " bytes";
             }
         }
         
         private void IncreaseStorage(long spaceRequest)
         {
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 long newSpace = isf.Quota + spaceRequest; 
                 try
                 {
                     if (true == isf.IncreaseQuotaTo(newSpace))
                     {
                         Results.Text = "Quota successfully increased.";
                     }
                     else
                     {
                         Results.Text = "Quota increase was unsuccessfull.";
                     }
                 }
                 catch (Exception e)
                 {
                     Results.Text = "An error occured: "+e.Message;
                 }
                 SetStorageData();
             }
         }
  
         private void Button_Click(object sender, RoutedEventArgs e)
         {
             try
             {
                 long spaceRequest = Convert.ToInt64(SpaceRequest.Text);
                 IncreaseStorage(spaceRequest);
             }
             catch { // User put bad data in text box }
         }
     }
 }

Thank you,

--Mike Snow

 Subscribe in a reader