[Windows Phone]All about Isolated Storage

A virtualized file system can only be accessed by your application. I think this is why it is called “Isolated”. XmlSerializer needs System.Xml.Serialization dll

Storage Options:
AP_Con_IStorage3

1. Create a text file

IsolatedStorageFile store = 
IsolatedStorageFile
.GetUserStoreForApplication(); IsolatedStorageFileStream file =
store.OpenFile("settings.txt",FileMode.OpenOrCreate); StreamWriter writer = new StreamWriter(file); writer.WriteLine("Text String");

2.Read text from file

IsolatedStorageFile store = 
IsolatedStorageFile
.GetUserStoreForApplication(); IsolatedStorageFileStream file =
store.OpenFile("settings.txt"FileMode.Open); StreamReader reader = new StreamReader(file); string content = reader.ReadToEnd();

3.Delete a file

string fileName = idString + ".xml";
using (IsolatedStorageFile store = 
IsolatedStorageFile
.GetUserStoreForApplication()) {     if (store.FileExists(fileName))     {          store.DeleteFile(fileName);     } }

4. Serialize object to xml and persist in isolated storage

IsolatedStorageFile store = 
IsolatedStorageFile
.GetUserStoreForApplication(); using (var file = store.CreateFile("fileName.xml")) {     XmlSerializer serializer = new XmlSerializer(typeof(Note));     serializer.Serialize(file, note); }

5. Deserialize xml to object

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
XmlSerializer serializer = new XmlSerializer(typeof(Note));
using (var file = store.OpenFile(fileName, FileMode.Open))
{
    note = (Note)serializer.Deserialize(file);
}

Please note that IsolatedStoregeFile and IO related objects should be closed or disposed. for example

writer.Close();
store.Dispose();

 

 

 

 

 


blog comments powered by Disqus