Saturday, April 26, 2008

Isolated Storage in Silverlight 2.0

Firstly, the default limit is 100 KB, but you can increase the quota. The quota increases to predefined levels: 100 KB, 1 MB, 5 MB, 10 MB or unlimited. E.g. when request to increase the isolated storage to 9 MB, it actually increases to 10 MB. The increase quota action must be user-initiated, e.g. a button click event, then a Yes/No dialog box will pop up to ask the user whether to allow the quota increase.

Dim store As IsolatedStorageFile
store = IsolatedStorageFile.GetUserStoreForApplication
dim result as Boolean = store.TryIncreaseQuotaTo(10 * 1024 * 1024) '2MB
If result = False Then DoSomeThing()
store.Dispose()

For the same Silverlight application, the same isolated storage is accessible from different web browsers, which is good. Regarding on how to save and retrieve data from isolated storage, it is exactly the same as Winforms.

Save data:
Dim store As IsolatedStorageFile
store = IsolatedStorageFile.GetUserStoreForApplication
Dim fs As New IsolatedStorageFileStream(fileName, IO.FileMode.Create, store)
Dim sw As New IO.StreamWriter(fs)
sw.Write(stringContent)
sw.Close()
fs.Dispose()
store.Dispose()


Read data:
Dim store As IsolatedStorageFile
store = IsolatedStorageFile.GetUserStoreForApplication
Dim fs As New IsolatedStorageFileStream(fileName, IO.FileMode.OpenOrCreate, store)
Dim sr As New IO.StreamReader(fs)
Dim s As String = sr.ReadToEnd
sr.Close()
fs.Dispose()
store.Dispose()

0 comments: