As of now we have learned about various file storing technique and in this post we will start with the first one in detail i.e. Storing Data Using WP7.1 Isolated Storage APIs. First of all we need to have a namespace so for this we have a namespace System.IO.IsolatedStorage. Here is a quick 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, which is basically used to store the settings of your application
Saving Data
Now we have understood the terminology then lets’s take a quick look on how data is stored. Below 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; }