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

Isolated Storage on WP7 (I)

Here I will explain how to store data on WP7 though it will be cleared after a compele OS boot.
Actually, all you need is an ApplicationSetting in IsolatedStorage.
Here create a class which you can set the ApplicatonSetting thus storing your data

    public class ApplicationSettings
    {
        private static string m_myAppKey = “myAppKey”;
        public static ApplicationData myAppSettings
        {
            get
            {
                return GetKeyValue<ApplicationData>(m_myAppKey);
            }
            set
            {
                SetKeyValue(m_myAppKey, value);
            }
        }

        private static T GetKeyValue<T>(string key)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
                return (T)IsolatedStorageSettings.ApplicationSettings[key];
            else
                return default(T);
        }

        private static void SetKeyValue<T>(string key, T value)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
                IsolatedStorageSettings.ApplicationSettings[key] = value;
            else
                IsolatedStorageSettings.ApplicationSettings.Add(key, value);

            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }

Then in code behind, forexample, I want to save a List called myList

In the saving part, what you have to write is

ApplicationSettings.myAppSettings = myList;

In the loading part, which you have to load the previously stored myList, just call

List myPreviousList = new List();
myPreviousList = ApplicationSettings.myAppSetting;

Of course, you may check if the myAppSetting is null or not before loading the data.

It is just one of the ways to store the data in WP7, tomorrow I will introduce another one. 🙂