This is the third post regarding Isolated Storage in Windows Phone app. In this post I am going to talk about How to store data in Text files in isolated storage in your app and perform different manipulation operations on it like reading , writing ,append , update and deletion of textfiles.
1. Create a new project Textfiles.
2. Now we will create a text file in isolated storage having the name Samplefile.txt.
3. But before starting with it you need to add the following namespace to your page in which you are using isolated storage code.
Basically we use IsolatedStorageFileStream class to read, write and create file in isolated storage. Since this class extends FileStream, you can use an instance of IsolatedStorageFileStream in most situations where a FileStream might otherwise be used, such as to construct a StreamReader or StreamWriter.
4. Add the following line of code which denotes the filename globally to the MainPage.xaml.cs.
Creating a Text file
In the following code given below we first create a text file and write some data to it.
Reading from a text file
In this segment of code we first open the text file and read the content from it and display it in the Textblock named Displayblock given below in the MainPage.
Write to the existing file
In the following lines of code we open the existing file and overwrite its content with new data.
Append to the existing file
This code is similar to the code given above for writing to the file. But there is only one difference that the file mode here chosen is Append not open.It writes the new data to the end of text file.
Delete the text file
Before deleting the file we check whether it is present in isolated storage or not .If it is present we delete it.
Note: The file which we have created above has been directly saved to the isolated storage.
5. When you are dealing with big projects and have large no of text files it is necessary that you should save them properly.A good practice is to create different folders/Directories and save the file in it.
Create a new directory
The code for different operations explained above on normal files will be same for the directory files. The only change will be in the name of file as shown above.
6. I hope this post will be helpful to you. You can download the full source project file from the link given below.
Download Full Project Textfiles.zip
Isolated Storage in Windows Phone app – Introduction
Isolated Storage in Windows phone app – Store data in IsolatedStorageSettings
Isolated Storage in Windows phone app – Reading and saving images