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 = _
        IsolatedStorageFile.GetUserStoreForApplication()';
     

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';
                  writer.Write(temp)';
            End Using';
     End Using';
End Using';

What have been written can be read

Using mystore As IsolatedStorageFile = _';
        IsolatedStorageFile.GetUserStoreForApplication()';
        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

image

same as XML with DataContractSerializer

image

Published Saturday, November 29, 2008 10:02 AM by preishuber
Filed under: , , ,

Comments

# B# and Silverlight Isolated Storage - Hannes Preishuber

Saturday, November 29, 2008 5:02 AM by B# and Silverlight Isolated Storage - Hannes Preishuber

Pingback from  B# and Silverlight Isolated Storage - Hannes Preishuber

# B# and Silverlight Isolated Storage | Silverlight Guide

Saturday, November 29, 2008 7:58 AM by B# and Silverlight Isolated Storage | Silverlight Guide

Pingback from  B# and Silverlight Isolated Storage | Silverlight Guide

# Silverlight Cream for November 29, 2008 -- #442

Sunday, November 30, 2008 2:24 AM by Community Blogs

In this issue: Hannes Preishuber, Tim Greenfield, Peter Bromberg, and Jonathan van de Veen. Shoutout

# re: B# and Silverlight Isolated Storage

Monday, December 01, 2008 9:09 AM by Matt Watson

LOL, just adding semicolons on the end isn't going to make it more readable for C# developers. Nice :-)

You should have taken it one step further on the End Using and put '}

# 2008 December 02 - Links for today « My (almost) Daily Links

Pingback from  2008 December 02 - Links for today « My (almost) Daily Links

# re: B# and Silverlight Isolated Storage

Tuesday, December 02, 2008 9:32 AM by JD

So, what's the difference between this and VB?

# re: B# and Silverlight Isolated Storage

Thursday, May 21, 2009 7:39 AM by Ronald

See this web app http://www.ToolToMeet.com to see other possibilities of using isolated storage

Leave a Comment

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