Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Isolated Storage for Windows Phone 7

DZone's Guide to

Isolated Storage for Windows Phone 7

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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");

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Jevgeni Tšaikin. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}