DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Isolated Storage for Windows Phone 7

Isolated Storage for Windows Phone 7

Jevgeni Tšaikin user avatar by
Jevgeni Tšaikin
·
Oct. 10, 11 · Mobile Zone · News
Like (0)
Save
Tweet
6.54K Views

Join the DZone community and get the full member experience.

Join For Free

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

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Regression Testing: Significance, Challenges, Best Practices and Tools
  • Data Science Project Folder Structure
  • 9 Strategies to Improve Your Software Development Process
  • SQL vs. NoSQL: Pros and Cons

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo