Isolated Storage on WP7 (II)

On the previous post, we learn how to store settings to Isolated Storage

if this time we have to store as a file, how can we do?
It is the second choice for you to store data on WP7 - IsolatedStorageFile

it is easy to code actually.

        public void SaveToFile(string content)
        {
            //Setting the fileName
            var fileName = "myWP7.dat";

            ///<summary>
            ///get the user Store and then create the file in the store
            ///finally write the content to the file also with a message box
            ///</summary>
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            using (var writeStream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
            using (var writer = new StreamWriter(writeStream))
            {
                writer.Write(content);
            }
            MessageBox.Show("Hello, " + content);
        }

Here you can see, what we have done is to save the content to a file called myWP7.dat

Probably, no exception can be caught, that’s why I haven’t put the try and catch there

If you read carefully, you will notice it is something like a HelloWorld, which store the user name inputted in the TextBox and we save it also prompt the Hello Message to the user

if it is the second time for user to use the application, we can simply load it from the UserStore, however, this time we have to use try-catch because we cannot ensure this is not the first time user use the app, which means the file might not exist in the UserStore

        public string LoadFromFile()
        {
            //Setting the fileName
            var fileName = "myWP7.dat";
            try
            {
                ///<summary>
                ///get the user Store and then open the file in the store
                ///finally read the content to the file and return it
                ///</summary>
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                using (var readStream = new IsolatedStorageFileStream(fileName, FileMode.Open, store))
                using (var reader = new StreamReader(readStream))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (IsolatedStorageException e)
            {
                //IsolatedStorageException catch if File cant be opened
                MessageBox.Show("What is your name?");
                return String.Empty;
            }
        }

Of course, you can play more on the IsolatedStorageFile.GetUserStoreForApplication which is the store variable here

you can Append, Delete files, etc etc.

Just to remember the Exception here is the IsolatedStorageException but not the FileNotFoundException

More Tutorials will be posted here :) Welcome to subscribe and follow me in Twitter

About these ads

11 thoughts on “Isolated Storage on WP7 (II)

  1. Thanks !

    I believe the extension of the filename to be saved is really important as well. I have tried .txt for quite several times and not success, until I have changed it to bare bone “data”; no extension then it hits me.

    Reply
  2. Hi Steve,

    Thanks for your article.
    I’ve created a small, free and opensource tool to explore IsolatedStorage without installing anything on the phone. Preview of text and image file are possible and you can also explore SqlCE database. Hope it’s help for developpment with Isolated Storage :)

    http://isostorespy.codeplex.com/

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s