Quick Tip: Isolated Storage in Silverlight 2 Beta 2
One of changes in new Silverlight 2 Beta 2, is new "Silverlight Configuration" screen.
Now we can take control over DRM and Application Storage. Application storage screen controls isolated storage data and quotas for application being used it:
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:
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