Storing Data Using WP7.1 Isolated Storage APIs

As of now we have learned about the various file storiage techniques and in this article we will start with the first one in detail. In other words, storing data using WP7.1 Isolated Storage APIs. First of all we need to have a namespace so for this we have the namespace "System.IO.IsolatedStorage". Here is a quick definition of terminology used in Isolated Storage:

  • IsolatedStorageFile: represents an isolated storage containing files and directories.
  • IsolatedFileStrem: exposes a file stream access to a file stored within Isolated Storage.
  • IsolatedStorageSettings: it is a very nice name/value pair dictionary, that is basically used to store the settings of your application.

Saving Data

Now that we have understand the terminology, lets's take a quick look at how data is stored. The following is the sample code: 

private void saveData(string message)

{

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

    {

        using (IsolatedStorageFileStream fstream = isf.CreateFile("Mydata.store"))

        {

            StreamWriter writer = new StreamWriter(fstream);

            writer.WriteLine(message);

            writer.Close();

        }

    }

}

First we created the IsolatedStorageFile object and then a IsolatedStorageFileStream to create our file, and then the old stuff by creating the StreamWriter object and passing the IsolatedStorageFileStream to it and then writing the message and closing it.

Loading Data

private string loadData()

{

    string result = null;

    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

    {

        if (isf.FileExists("MyData.store"))

        {

            using (IsolatedStorageFileStream fstream = isf.OpenFile("MyData.store", System.IO.FileMode.Open))

            {

                StreamReader reader = new StreamReader(fstream);

                result = reader.ReadLine();

                reader.Close();

            }

        }

    }

    return result;

 }


Similar Articles