Mobile Zone is brought to you in partnership with:

I have worked for several companies as a senior software developer and lead developer. I bave enterprise level programming experience on Web, Silverlight, and Windows Phone 7. I am a MVP in Device Application Development since 2011. Jevgeni is a DZone MVB and is not an employee of DZone and has posted 40 posts at DZone. You can read more from them at their website. View Full User Profile

Isolated Storage for Windows Phone 7

10.10.2011
| 4268 views |
  • submit to reddit

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 Overview for Windows Phone at MSDN
  • Isolated Storage Best Practices for Windows Phone at MSDN
  • Using Isolated Storage on Windows Phone 7 (by Brad Tutterow)

    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");
References
Published at DZone with permission of Jevgeni Tšaikin, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Gagan Peter replied on Sat, 2012/04/14 - 4:30am

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?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.