We can use Isolated Storage as a virtual file system to store data in a hidden folder on our machine in silverlight application. Silverlight application is allocated the storage of 1 MB per application but we can increase the amount of storage.
|
This is the function to get the data in the isolated storage
|
Private Function LoadData(ByVal fileName As String) As String Dim data As String = String.Empty Dim isfstream As New IsolatedStorageFileStream(fileName, FileMode.Open, IsolateStorageFileObj) Dim sr As New StreamReader(isfstream) data = sr.ReadLine() Return data End Function This is code snippet in vb.net to store and fetch the data from isolated storage. Imports System.IO Imports System.IO.IsolatedStorage 'IsolatedStorageFile.GetUserStoreForApplication() function get the isolated storage for the application Private IsolateStorageFileObj As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication() Private Sub SaveDatabtn_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 'Calls the Save data function to save the input text in IsolatedStorage.txt file SaveData(InputDataTxt.Text, "IsolatedStorage.txt") End Sub Private Sub GetDatabtn_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Dim Data As String = String.Empty Data = LoadData("IsolatedStorage.txt") 'Gets the data from the isolated storage and show MessageBox.Show(Data) End Sub This is the function to save the data in the isolated storage Private Sub SaveData(ByVal data As String, ByVal fileName As String) Dim isfstream As New IsolatedStorageFileStream(fileName, FileMode.Create, IsolateStorageFileObj) 'Delete if the file already exists If IsolateStorageFileObj.FileExists(fileName) = True Then IsolateStorageFileObj.DeleteFile(fileName) End If Dim sw As New StreamWriter(isfstream) sw.Write(data) sw.Close() End Sub