Isolated Storage for Windows Phone 7

I have created a small Windows Phone Isolated Storage helper-class. It contains methods for creating/deleting directories and writing/reading/deleting files. Each of those methods is static and have enough comments to understand the usage. Same class can be also used in Silverlight applications. Feel free using it in your application

eugenedotnet isolated storage windows phone 7

Additional information

Isolated Storage Helper

    public class IsolatedStorageHelper
    {
        /// <summary>
        /// method for creating a directory within isolated storage
        /// </summary>
        /// <param name="directoryName">Name of a directory to be created</param>
        public static void CreateDirectory(string directoryName)
        {
            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!string.IsNullOrEmpty(directoryName) && !currentIsolatedStorage.DirectoryExists(directoryName))
                    {
                        currentIsolatedStorage.CreateDirectory(directoryName);
                    }
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }
        }

        /// <summary>
        /// Method for deleting an isolated storage directory
        /// </summary>
        /// <param name="directoryName">Name of a directory to be deleted</param>
        public static void DeleteDirectory(string directoryName)
        {
            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!string.IsNullOrEmpty(directoryName) && currentIsolatedStorage.DirectoryExists(directoryName))
                    {
                        currentIsolatedStorage.DeleteDirectory(directoryName);
                    }
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }
        }

        /// <summary>
        /// Method for creating a file in isolated storage
        /// </summary>
        /// <param name="directoryName">Path to directory</param>
        /// <param name="fileNameWithExtention">File name with extention</param>
        /// <param name="content">Content for a file</param>
        /// <param name="createNew">Indicates if a previous version of a file should be deleted before the creation</param>
        public static void CreateFile(string directoryName, string fileNameWithExtention, string content, bool createNew)
        {
            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // if file name was not specified then do not create a file
                    if (string.IsNullOrEmpty(fileNameWithExtention))
                        return;

                    string filePath = GetFilePath(directoryName, fileNameWithExtention);

                    if (createNew)
                    {
                        if (currentIsolatedStorage.FileExists(filePath))
                        {
                            // if file exists - delete it before creating a new one
                            currentIsolatedStorage.DeleteFile(filePath);
                        }
                    }

                    // open writer stream to write a content to a file
                    StreamWriter destinationFile = new StreamWriter(new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, currentIsolatedStorage));

                    // write content to a file in isolated storage
                    destinationFile.WriteLine(content);

                    // close writer stream
                    destinationFile.Close();
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }
        }

        public static string ReadFile(string directoryName, string fileNameWithExtention)
        {
            string content = null;

            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!string.IsNullOrEmpty(fileNameWithExtention))
                    {
                        // open a reader stream to read a file content for isolated storage
                        StreamReader fileToRead = new StreamReader(new IsolatedStorageFileStream(
                            GetFilePath(directoryName, fileNameWithExtention)
                            , FileMode.Open
                            , currentIsolatedStorage));

                        content = fileToRead.ReadLine();

                        // close reader stream
                        fileToRead.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }

            return content;
        }

        /// <summary>
        /// Method for deleting a single file from Isolated Storage
        /// </summary>
        /// <param name="directoryName">Directory path for a file</param>
        /// <param name="fileNameWithExtention">File name with extention</param>
        public static void DeleteFile(string directoryName, string fileNameWithExtention)
        {
            try
            {
                using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!string.IsNullOrEmpty(fileNameWithExtention))
                    {
                        currentIsolatedStorage.DeleteFile(GetFilePath(directoryName, fileNameWithExtention));
                    }
                }
            }
            catch (Exception ex)
            {
                // do something with exception
            }
        }

        /// <summary>
        /// Utility method that returns a file path based on directory name and input file name parameter
        /// </summary>
        public static string GetFilePath(string directoryName, string fileNameWithExtention)
        {
            if (directoryName != null && !directoryName.EndsWith("\\"))
                directoryName += "\\";

            return (directoryName + fileNameWithExtention);
        }
    }

Methods are tested using following code:

// testing without a directory
IsolatedStorageHelper.CreateFile(null, "test.txt", "this is a test", true);
string fileContent = IsolatedStorageHelper.ReadFile(null, "test.txt");
IsolatedStorageHelper.DeleteFile(null, "test.txt");

// testing with directory specified
IsolatedStorageHelper.CreateDirectory("testdir");
IsolatedStorageHelper.CreateFile("testdir", "test2.txt", "this is a test 2", true);
string fileContent2 = IsolatedStorageHelper.ReadFile("testdir", "test2.txt");
IsolatedStorageHelper.DeleteFile("testdir", "test2.txt");
IsolatedStorageHelper.DeleteDirectory("testdir");





This entry was posted in Development, Silverlight, Windows Phone 7 and tagged , , . Bookmark the permalink.

4 Responses to Isolated Storage for Windows Phone 7

  1. Luis Silva says:

    hello,
    My name is Luís and i start to devolop aplication in WP7. I have problems in line 78
    // open writer stream to write a content to a file
    StreamWriter destinationFile = new StreamWriter(new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, currentIsolatedStorage));

    i already try to make that:
    using System.IO.IsolatedStorage;
    using System.IO.IsolatedStorage.IsolatedFileStream;
    but i have a error because said the type or name “IsolatedFileStream” doesn’t exist in System.IO.IsolatedStorage.
    How i can solve that?

    Best regards,
    Luís

    • eugenedotnet says:

      Try using “IsolatedStorageFileStream” instead of “IsolatedFileStream”.

      • Luis Silva says:

        hello,

        thank’s for the reply, but i used IsolatedStorageFileStream, i only miss in description in the error..
        this is my actual class code only for testing your class file:

        using System;
        using System.Net;
        using System.Windows;
        using System.Windows.Controls;
        using System.Windows.Documents;
        using System.Windows.Ink;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Windows.Media.Animation;
        using System.Windows.Shapes;
        using System.IO.IsolatedStorage;
        using System.IO.StreamReader;
        using System.IO.StreamWriter;

        namespace helloworld
        {
        public class IsolatedStorageHelper
        {
        ///
        /// method for creating a directory within isolated storage
        ///
        /// Name of a directory to be created
        public static void CreateDirectory(string directoryName)
        {
        try
        {
        using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
        if (!string.IsNullOrEmpty(directoryName) && !currentIsolatedStorage.DirectoryExists(directoryName))
        {
        currentIsolatedStorage.CreateDirectory(directoryName);
        }

        }
        }
        catch (Exception ex)
        {
        //

        }
        }

        /// Name of a directory to be deleted
        public static void DeleteDirectory(string directoryName)
        {
        try
        {
        using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
        if (!string.IsNullOrEmpty(directoryName) && currentIsolatedStorage.DirectoryExists(directoryName))
        {
        currentIsolatedStorage.DeleteDirectory(directoryName);
        }
        }
        }
        catch (Exception ex)
        {
        //

        }

        }
        ///
        /// Method for creating a file in isolated storage
        ///
        /// /// Path to directory
        /// File name with extention
        /// Content for a file
        /// /// Indicates if a previous version of a file should be deleted

        public static void CreateFile(string directoryName, string fileNameWithExtention, string content, bool createNew)
        {
        try
        {

        using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
        // if file name was not specified then do not create a file
        if (string.IsNullOrEmpty(fileNameWithExtention))
        return;
        string filePath = GetFilePath(directoryName, fileNameWithExtention);
        if (createNew)
        {
        if (currentIsolatedStorage.FileExists(filePath))
        {
        // if file exists – delete it before creating a new one
        currentIsolatedStorage.DeleteFile(filePath);
        }
        }
        StreamWriter destinationFile = new StreamWriter(new IsolatedFileStream(filePath, FileMode.OpenOrCreate, currentIsolatedStorage));

        }

        }
        catch (Exception ex)
        {
        //

        }

        }

        }

        }
        Regards

        • eugenedotnet says:

          Try this code:

          using System;
          using System.Net;
          using System.Windows;
          using System.Windows.Controls;
          using System.Windows.Documents;
          using System.Windows.Ink;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Windows.Media.Animation;
          using System.Windows.Shapes;
          using System.IO.IsolatedStorage;
          using System.IO;

          namespace helloworld
          {
          public class IsolatedStorageHelper
          {
          ///
          /// method for creating a directory within isolated storage
          ///
          /// Name of a directory to be created
          public static void CreateDirectory(string directoryName)
          {
          try
          {
          using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
          {
          if (!string.IsNullOrEmpty(directoryName) && !currentIsolatedStorage.DirectoryExists(directoryName))
          {
          currentIsolatedStorage.CreateDirectory(directoryName);
          }

          }
          }
          catch (Exception ex)
          {
          //

          }
          }

          /// Name of a directory to be deleted
          public static void DeleteDirectory(string directoryName)
          {
          try
          {
          using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
          {
          if (!string.IsNullOrEmpty(directoryName) && currentIsolatedStorage.DirectoryExists(directoryName))
          {
          currentIsolatedStorage.DeleteDirectory(directoryName);
          }
          }
          }
          catch (Exception ex)
          {
          //

          }

          }
          ///
          /// Method for creating a file in isolated storage
          ///
          /// /// Path to directory
          /// File name with extention
          /// Content for a file
          /// /// Indicates if a previous version of a file should be deleted

          public static void CreateFile(string directoryName, string fileNameWithExtention, string content, bool createNew)
          {
          try
          {

          using (IsolatedStorageFile currentIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
          {
          // if file name was not specified then do not create a file
          if (string.IsNullOrEmpty(fileNameWithExtention))
          return;
          string filePath = GetFilePath(directoryName, fileNameWithExtention);
          if (createNew)
          {
          if (currentIsolatedStorage.FileExists(filePath))
          {
          // if file exists – delete it before creating a new one
          currentIsolatedStorage.DeleteFile(filePath);
          }
          }
          StreamWriter destinationFile = new StreamWriter(new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, currentIsolatedStorage));

          }

          }
          catch (Exception ex)
          {
          //

          }

          }

          public static string GetFilePath(string directoryName, string fileNameWithExtention)
          {
          if (directoryName != null && !directoryName.EndsWith(“\\”))
          directoryName += “\\”;

          return (directoryName + fileNameWithExtention);
          }

          }

          }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comments links could be nofollow free.

Notify me of followup comments via e-mail. You can also subscribe without commenting.