I hope you will find this lesson more interesting as most developers think about phone storage available on devices for their applications and how they can utilize it. Each application will have its own isolated local storage. It is independent from the other applications and OS files and can be only accessed by your application.

Before we move on, you need to have a basic understanding of event handling. I went through this in Lesson 1. Please finish it first before moving on.

Two questions would pop in your head, one would be about utilizing storage for your app on an SD card, and the answer yes and the next question would be about the size limit, so as I know (and I know little Smile) you will have virtually no constraints on the application storage capacity. So as long as you have storage available in your device then your app can utilize it.

The idea of our application is very simple, I will use the local storage to read/write a string message and a counter to a text file. I will be increasing the counter every time we launch the application (aka load the page) and will display the last message stored in the text file.

You can always find the lesson source code attached at the end of the blog post.

So, How does it look like?

image

As you figured we used 2 TextBlocks to display the message and the counter and one TextBox to capture the new message if any. So what about the XAML behind this, well as always StackPanel Winking smile is my hero:

image

In this scenario we named the TextBlocks because we will be pushing content to them from our code behind. So lets start with what happens when the page loads.

So what happens when the page Loads?

Before we start with the page load logic, please make sure to add the following using statements on the top of the page to be able to use the isolated storage and carry on file operations.

image

On page load I need to check if the file I use to store the string and counter exists or not. If it does exist I read the counter and the message and push them to global variables. If the file does not exist, I write 0 and “No messages..!” in a new file. The file I will be using will be called LS.txt (short for Local Storage), I know very impressive naming skills Smile with tongue out.

Finally, I push the values to the TextBlocks by replacing the “##” symbols in my string with the variable values using the string.Replace function.

image

and when it Unloads?

when the page unloads or navigated away from using the Back or Start buttons, we replace the current LS.txt file with the new content. First we increment the counter and then add the message.

image

The final part of our application is saving the new message and exiting the application so that we call the OnNavigatedFrom handler.

Save New Message

We push the contents of the TextBox back to our variable and push a message box to the user.

image

Let’s Test it out..!

Run the emulator using the F5 key and please be patient until Visual Studio manages to compile, build, and deploy our application.

The application loads with the counter set to 0 and I input a message.

image

Now I will hit save and the application will display the message box. We will exit when the message is dismissed which will call the unloaded and increment the counter and save my new message.

image

Once you hit back after dismissing the application, go to the menu again on the phone and click on Lesson 2.

image

and taraaaaaaaaaaaaaaaaaa..!! Smile we have an updated counter and our last message:

image

I hope enjoyed your lesson today and have an idea on how to read and write files in Windows Phone.

Let me know if you get stuck somewhere.