Introduction:
In this article we are going to see the most important topic on the Windows Phone 7 Development, which is the Isolated Storage for Files and Folders. Typically any application development requires a location to store the user data locally to manipulate, here with Windows Phone 7 development we have the Isolated Storage to store the data in a isolated location which is accessible to that particular application only. In our previous article we have seen what Isolated Storage is for Windows Phone 7 Development and also we have seen about how to use the dictionary to store data locally in key value pair. [more]
In this tutorial we are going to see how to perform the below tasks on the Isolated Storage for Files and Folders.
- Write some text to a text file
- Read the text file
- Delete the text file
- Write some text to a text file inside a folder
- Read the text file inside a folder
Let us jump start to see the step by step process on how to use the Files and Folders to store the data locally with Windows Phone 7 device.
Steps:
Open Visual Studio 2010 IDE as an administrator and create a new Silverlight for Windows Phone 7 project with a valid project name as shown in the screen below.
IsolatedStorageFileStream class is used in Windows Phone 7 Development to read, write and create files in an Isolated Storage, we can create an instance of this class to get the stream of object to read and write using the StreamReader and StreamWriter. To start with let us first design a unique interface by using the controls from the tool box, once the design is completed we can see the screen as shown in the screen below. Just copy the XAML Code below to get the same look and feel.
XAML Code:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now we are done with the design, let us start with the code behind to write the core logic to work with files and folders. First we need to import the namespace required for the Isolated Storage by adding the below 2 namespaces to the code behind.
Code Behind:
|
|
|
|
Task 1 – Write text to a text file
Now our first task is to write some data to the text file and store it to the isolated storage. We need to use the IsolatedStorageFile class instance to get the user store for the specific application and to write the file we need to use the IsolatedStorageFileStream by providing the parameters that are required to store the details locally as shown in the code below.
Code Behind:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 2 – Read the text file
Our next task is to read the text file from the root folder where we saved the text data in our previous task. For this task as well we need to use the IsolatedStorageFile Class and IsolatedStorageFileStream to get the data as shown in the code below.
Code Behind:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 3 – Delete the text file
In this task we are going to see how to delete the text file from the Isolated Storage which we created in our previous tasks. To delete the file we need to make use of the IsolatedStorageFile class and invoke the DeleteFile method by passing the filename directly as shown in the code below. Also to delete a folder we can use the DeleteDirectory method which do the similar process of deleting the directory completely.
Code Behind:
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 4 – Write some text to a text file inside a folder
Our next task is to write some text to a text file and save the file to a specific folder for easy data file maintenance with in the Isolated Storage. Normally this task is very important for developing the application with Windows Phone 7 to store the data locally in a folder to a specific folder. Copy the below code directly to the button click event to complete the task.
Code Behind:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 5 – Read the text file inside a folder
Our final task is to read the file which we create and saved in a particular folder in our previous task. This task uses the same IsolatedStorageFile and IsolatedStorageFileStream to read the data as shown in the code below.
Code Behind:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now we are done with the complete tasks and we can see the expected outputs as shown in the screen below. To build and execute the project simply press F5 from the keyboard or from the Visual Studio IDE tool bar.
Outputs Screen:
Conclusion:
So in this article we have seen the Files and Folders in Isolated Storage for Windows Phone 7 development using the IsolatedStorageFile class. In our next article we will see how to use the local storage database to save the data to read and write when ever required from the application.