Isolated Storage is a place on the disk where your .NET application always has write permissions. That makes it ideal for applications that need to store information such as settings or XML without first asking for permission. Application types such as ClickOnce or non-installable applications would benefit hugely from this capability.

It is a little cumbersome to use, because you do not have directly IO access to the Isolated Storage folder. Instead you have to use an IsolatedStorageFileStream and that is a little more complicated than simple file IO. That’s why I always use a static helper class that does the dirty work for me. I use it mainly to write/read applications settings or as data storage for XML files.

This is the class:

#region Using

 

using System.IO;

using System.IO.IsolatedStorage;

 

#endregion

 

namespace Google_Positioner

{

  public static class StorageHelper

  {

 

    /// <summary>

    /// Saves the content to the file.

    /// </summary>s

    public static void Save(string fileName, string content)

    {

      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))

      {

        using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(fileName, FileMode.Create, isoStore)))

        {

          writer.Write(content);

        }

      }

    }

 

    /// <summary>

    /// Loads the content of the file.

    /// </summary>

    public static string Load(string fileName)

    {

      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))

      {

        if (isoStore.GetFileNames(fileName).Length > 0)

        {

          using (StreamReader reader = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, isoStore)))

          {

            return reader.ReadToEnd();

          }

        }

      }

 

      return null;

    }

 

    /// <summary>

    /// Deletes the file to clear the settings.

    /// </summary>

    public static void Delete(string fileName)

    {

      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))

      {

        if (isoStore.GetFileNames(fileName).Length > 0)

          isoStore.DeleteFile(fileName);

      }

    }

 

  }

}

Example

You can use it to write XML files like I do in this example:

protected override void DataInsert()

{

  StringBuilder sb = new StringBuilder();

  using (XmlWriter writer = XmlWriter.Create(sb))

  {

    writer.WriteStartDocument(true);

    writer.WriteStartElement("Webpage");

    writer.WriteElementString("Name", this.Name);

    writer.WriteElementString("Url", this.Url.ToString());

    writer.WriteEndElement();

  }

 

  StorageHelper.Save(FileName, sb.ToString());

}

And to retrieve the XML file, you can do it like so:

>

protected XmlDocument GetXmlFile()

{

  string content = StorageHelper.Load(FileName);

  if (!string.IsNullOrEmpty(content))

  {

    XmlDocument doc = new XmlDocument();

    doc.LoadXml(content);

 

    return doc;

  }

}

Comments


Comments are closed