Isolated Storage in Silverlight


HTML clipboard

Silverlight applications can not access the file system of the client machine due to the secure sandbox where it execute in the web browser .But if the silverlight application wants to store the information on the client machine for later retrieval they can use a feature called the Isolated Storage.

Isolated Storage refers to the restricted area of the file sytem to which certain restrictions apply.For instance application do not know where on the hard disk there information is being stored.And the information on the Isolated storage that is stored by one application can not be accessed by another application.

Isolated storage is what persistent cookies were in traditional web applications.The hard disk space to which isolated storage refers has certain checks in place to prevent malicious attacks.Default space limit of Isolated storage is 1 MB.

Scope of Isolated storage is application and user specific.In other words same silverlight application can have multiple isolated storage for different users.The information in the Isolated storage is persistent that means it never expires and if the user deletes the temporary internet files it is not deleted.

The types in the System.IO.IsolatedStorage namespace which is a core part of the silverlight runtime is used for the Isolated storage .Silverlight applications automatically creates the Isolated store.For interacting with the Isolated storage we use the IsolatedStorageFile class.

We get the IsolatedStorageFile object for the current user and application by using the IsolatedStorageFile's GetUserStoreForApplication() method.

Isolated storage file refers to the collection of files in the Isolated Storage not to single file.

Reading and writing Isolated Storage.

To read and write files in Isolated Storage we use the normal c# stream read write operations.

First of all we need to retrieve the IsolatedStorage object by using the GetUserStoreForApplication static method.Once we have the IsolatedStorageFile object we are ready to work with the isolated storage.

In the below example we get the StreamWriter object from the IsolatedStorageFileStream by passing it to the constructor of the StreamWriter so we can perform normal string based operations.

IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
file.CreateFile("data.txt");
using (IsolatedStorageFileStream fs = file.CreateFile("data.txt"))
{
  StreamWriter wi = new StreamWriter(fs);
  wi.WriteLine("Sample text........");
  wi.Close();
 }

 using (IsolatedStorageFileStream fs = file.OpenFile("data.txt", FileMode.Open))
 {
   StreamReader re = new StreamReader(fs);
   string line = re.ReadLine();
   txtData.Text = line;
   Console.WriteLine(line);
   wi.Close();
 }

Managing the IsolatedStorage size

Every silverlight application initially gets 1 MB space . IsolatedStorageFile.AvailableFree shows the total free space available.But if the application wants more space it can request for more IsolatedStorage space by using the IsolatedStorageFile IncreaseQuotaTo() method .When we call IsolatedStorageFile IncreaseQuotaTo() method then a dialog box is displayed which gives user the option to allow or deny the IsolatedStorage size increase.

isolated.gif

Serializing objects with Xml Serializer

XmlSeriallizer is another option to serialize and deserialize objects rather than individual pieces of data using the StreamReader and StreamWriter .

XmlSerializer can serialize objects into bytes and deserialize bytes back to the object.

To use XmlSerializer we need to assure two things for a class:

  1. The class must have public no argument or default constructor.
  2. The class should have public settable properties.
To use XmlSerialization we need to include the reference to System.Xml.Serialization.dll.

If we have an instance of Employee class and a stream referenence we can serialize it using the following code:

private XmlSerializer serializer = new XmlSerializer(typeof(Employee));
serializer.Serialize(streamWriter, employee);

So Isolated Storage is a good place to store user information so it can be accessed over multiple user sessions.
 


Similar Articles