Quick Tip: Isolated Storage in Silverlight 2 Beta 2 - DevCorner

Quick Tip: Isolated Storage in Silverlight 2 Beta 2

One of changes in new Silverlight 2 Beta 2, is new "Silverlight Configuration" screen.

image

 

Now we can take control over DRM and Application Storage. Application storage screen controls isolated storage data and quotas for application being used it:

image 

But what could be stored in this storage? What if default quota (1Mb) is not enough? Could it been changed?

 

The isolated storage could store files and application settings. Working with isolated storage is pretty much like with file system:

First of all we need to get IsolatedStorage for application:

var store = IsolatedStorageFile.GetUserStoreForApplication()

Then, we could use it to create directories, subdirectories, files, etc.

store.CreateDirectory("MyApp1");
string subdirectory1 = System.IO.Path.Combine("MyApp1", "SubDir1");
store.CreateDirectory(subdirectory1);
IsolatedStorageFileStream rootFile = store.CreateFile("InTheRoot.txt");
rootFile.Close();

//Write to text file
string filePath = System.IO.Path.Combine(subdirectory1, "MyApp1A.txt");
try
{
   using (StreamWriter sw =
            new StreamWriter(store.OpenFile(filePath,
            FileMode.Open, FileAccess.Write)))
            {
                sw.WriteLine("To do list:");
                sw.WriteLine("1. Buy supplies.");
             }
}
catch (IsolatedStorageException ex)
{

    sb.AppendLine(ex.Message);
}

//Read from text file
try
{
    using (StreamReader reader =
        new StreamReader(store.OpenFile(filePath,
            FileMode.Open, FileAccess.Read)))
    {
        string contents = reader.ReadToEnd();
        sb.AppendLine(filePath + " contents:");
        sb.AppendLine(contents);
    }
}
catch (IsolatedStorageException ex)
{

    sb.AppendLine(ex.Message);
}

 

To use Isolated storage as application data store, we need to get IsolatedStorageSettings.ApplicationSettings:

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

Now we can use it to add/change/read/delete our applciation settings:

//Add new appSetting
appSettings.Add("email", "alexg@sela.co.il");
//Read appSetting
string mailAddress = (string)appSettings["email"];
//Change existing appSetting
appSettings["email"] = "alex@somemail.com";
//and finally delete it...
appSettings.Remove("email");

Another nice feature, is ability to increase Isolated Storage quota for application. To do it, we need to execute IncreaseQuotaTo() function. While trying to increase the Quota, user will get prompt, which need to be either approved or declined by him:

image

Here the code goes:

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    // Request 5MB more space in bytes.
    Int64 spaceToAdd = 5242880;
    Int64 curAvail = store.AvailableFreeSpace;

    // If available space is less than
    // what is requested, try to increase.
    if (curAvail < spaceToAdd)
    {

        // Request more quota space.
        if (!store.IncreaseQuotaTo(store.Quota + spaceToAdd))
        {
            // The user clicked NO to the
            // host's prompt to approve the quota increase.
            tbResults.Text = "User declined to approve Quota inrease";
        }
        else
        {
            // The user clicked YES to the
            // host's prompt to approve the quota increase.
            tbResults.Text = "Quota inreased";
        }
    }
}

 

Sample application could be found here.

Silverlight 2 Beta 2 links here.

 

Enjoy,

Alex

Published Sunday, June 08, 2008 9:42 AM by Alex Golesh

Comments

# re: Quick Tip: Isolated Storage in Silverlight 2 Beta 2

Hi Alex,

the right click stuff is not working with Beta 2. Can you take a look?

Thanks

Nick

Wednesday, June 11, 2008 5:46 PM by Nick Polyak

# re: Quick Tip: Isolated Storage in Silverlight 2 Beta 2

Hey,

I figured it out: the Silverlight.js file has changed.

Kol Tov

Wednesday, June 11, 2008 6:06 PM by Nick Polyak

# Silverlight Cream for July 16, 2008 -- #326

Corrina Barber updated Red &amp; Black skins, Mike Snow on Isolated Storage, Tim Heuer on S3 and SL2B2

Thursday, July 17, 2008 8:44 AM by Community Blogs

# re: Quick Tip: Isolated Storage in Silverlight 2 Beta 2

I can increase quota size up to 100MB

what's limit size of quota?

Wednesday, August 13, 2008 5:12 AM by LeMinh

# Silverlight Isolated Storage Maximum Quota - DevCorner

Pingback from  Silverlight Isolated Storage Maximum Quota - DevCorner

Thursday, August 14, 2008 12:35 AM by Silverlight Isolated Storage Maximum Quota - DevCorner

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: