B# and Silverlight Isolated Storage

I am a VB developer. As most of expert coder are using C# I must step forward. B# is the language which can be written by VB guys and make it readable for C# gurus. Today's topic is the data store of Silverlight on client side- isolated storage.

Isolated Storage is a concept from .NET. It allows to store information on a per application base, like cookies. Major differences are much more space and more security. There exists several ways to store data. First option is like ASP.NET appsettings.

 Private appSettings As IsolatedStorageSettings = _
           IsolatedStorageSettings.ApplicationSettings ';

Based on a Key Object collection you can store simple data like strings or also complex data like generic lists.

 Dim liste As New List(Of tasks)';
 appSettings.Add("daten", liste) ';
 appSettings.Add("test", New Date)';
 appSettings.Add("hannesKey", "Hannes Preishuber")';

Reading is as simple

 ausgabe.Text = appSettings("hannesKey")';

Isolated storage can also be used like a file system to store images or xaml. Code is written with B#.

Using mystore As IsolatedStorageFile = _

There are a lot of samples in web which shows file read and write access. I will top that with storing objects as file. For that object must be serialized. Silverlight supports XAML with DataContractSerializer class or JSON with DataContractJsonSerializer. For the last one you need to reference System.ServiceModel.Web.dll. With following code you can write the generic list as stream.

 Dim memStream As New MemoryStream';
 Dim ser As New DataContractJsonSerializer(liste.GetType)';
 ser.WriteObject(memStream, liste)';
 memStream.Position = 0';
 Dim mystore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()';
 Using reader As New StreamReader(memStream)';
    Using writeStream As New IsolatedStorageFileStream("meinObjekt.txt", _
FileMode.Create, mystore)';
           Using writer As New StreamWriter(writeStream)';
                  Dim temp As String = reader.ReadToEnd';
            End Using';
     End Using';
End Using';

What have been written can be read

Using mystore As IsolatedStorageFile = _';
        Using fStream As New IsolatedStorageFileStream( _';
              "meinObjekt.txt", FileMode.OpenOrCreate, mystore)';
            Using sr As New StreamReader(fStream)';
                    ausgabe.Text = sr.ReadToEnd()';
            End Using';
        End Using';
End Using';

The sample writes to Textblock as JSON


same as XML with DataContractSerializer



Comments have been disabled for this content.