All about WP7 Isolated Storage - Read and Save Text files
published on: 3/21/2011 | Views: N/A | Tags: IsoStore
by WindowsPhoneGeek
This is the fourth article from the “All about WP7 Isolated Storage ” series of short articles focused on real practical examples with source code rather than a plain theory. I am going to talk about how to read and save Text file into Isolated Storage.
- All about WP7 Isolated Storage – intro to Isolated Storage
- All about WP7 Isolated Storage - Folders and Files
- All about WP7 Isolated Storage - Store data in IsolatedStorageSettings
- All about WP7 Isolated Storage - Read and Save Text files
- All about WP7 Isolated Storage - Read and Save XML files using XmlSerializer
- All about WP7 Isolated Storage - Read and Save XML files using XmlWriter
- All about WP7 Isolated Storage - Read and Save Images
- All about WP7 Isolated Storage - Read and Save Captured Image
- All about WP7 Isolated Storage - Read and Save Binary files
- All about WP7 Isolated Storage - File manipulations
- All about WP7 Isolated Storage - Recommendations and Best Practices
- All about WP7 Isolated Storage - open source Databases and Helper libraries
To begin with lets first create a sample Windows Phone 7 application project. Next include the following namespaces in MainPage.xaml.cs (alternatively you can use the code in another page):
using System.IO; using System.IO.IsolatedStorage;
Reading and saving files to the Isolated Storage is a common task for many WP7 applications.
Basically we use IsolatedStorageFileStream class to to read, write and create files 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.
Save New Text File to Isolated Storage
In this example at first we create a new myFile.txt text file in the Isolated Storage and after that write some string content into it.
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//create new file
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("myFile.txt", FileMode.Create, FileAccess.Write, myIsolatedStorage)))
{
string someTextData = "This is some text data to be saved in a new text file in the IsolatedStorage!";
writeFile.WriteLine(someTextData);
writeFile.Close();
}
NOTE: When creating new file we use FileMode.Create, when we want to write to a file we use FileAccess.Write! You can find a list of all FileAccess possibilities here.
Write to existing Text File in Isolated Storage
In this example at first open an existing myFile.txt text file from the Isolated Storage and after that write some additional string content into it.
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Open existing file
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Write);
using (StreamWriter writer = new StreamWriter(fileStream))
{
string someTextData = "Some More TEXT Added: !";
writer.Write(someTextData);
writer.Close();
}
NOTE: When open an existing file from the Isolated Storage we use FileMode.Open, when we want to write to a file we use FileAccess.Write! You can find a list of all FileAccess possibilities here.
Read Text File from Isolated Storage
In this example at first open an existing myFile.txt text file from the Isolated Storage and read its content. After that the content is visualized in a TextBlock.
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{ //Visualize the text data in a TextBlock text
this.text.Text = reader.ReadLine();
}
NOTE: When open an existing file from the Isolated Storage we use FileMode.Open, when we want to read to a file we use FileAccess.Read! You can find a list of all FileAccess possibilities here.
Reading/Writing Text File in a Directory of the Isolated Storage
- Writing
//Obtain the virtual store for application
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new folder and call it "ImageFolder"
myIsolatedStorage.CreateDirectory("TextFilesFolder");
//Create a new file and assign a StreamWriter to the store and this new file (myFile.txt)
//Also take the text contents from the txtWrite control and write it to myFile.txt
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("TextFilesFolder\\myNewFile.txt", FileMode.OpenOrCreate, myIsolatedStorage));
string someTextData = "This is some text data to be saved in a new text file in the IsolatedStorage!";
writeFile.WriteLine(someTextData);
writeFile.Close();
- Reading
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("TextFilesFolder\\myNewFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{
this.text1.Text = reader.ReadLine();
}
NOTE: Take a look at All about WP7 Isolated Storage - Files and Folders for more info about directories.
Best Practices
1.) When working with files always use Using statement because it provides a convenient syntax that ensures the correct use of IDisposable objects.
2.) Always check if a Directory in which you want to create/read a File exists.
3.) Check for existing file before trying to read it

In this article I talked about reading and saving Text file into Isolated Storage. Here is the full source code.
Stay tuned with the rest of the posts. I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Formatting mistake
posted by: Gary Davis on 3/21/2011 9:29:04 PM
Looks like the headers for example 2 and 3 are flipped. Ex. 2 header says it is about Read but the code is about Write.
Gary
RE:Formatting mistake
posted by: winphonegeek on 3/21/2011 9:34:40 PM
Thank you for pointing that out. The headers are already fixed.
Where is the file?
posted by: Nafe on 4/1/2011 12:12:43 AM
Hi man, it's imazing, but where is the file? "myFile.txt" ??
RE: @Where is the file?
posted by: winphonegeek on 4/10/2011 1:13:08 PM
"myFile.txt" is created as a new file in the IsolatedStorage and its data is generated at run time. Alternatively you could use a file placed incise your project. In this case you will need to: at first read the existing content of the file, after that create a new file in the IsolatedStorage and finally copy the read content into the newly created file.
Read from txt file in a project
posted by: umonkey on 5/4/2011 9:03:20 AM
Hi guys, What happened to the data in Isolated Storage txt file after emulator is crashed? If I am trying to display it using:
IsolatedStorageFile myIsolatedStorage= IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("ImageFolder\myFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{
this.textBlock1.Text += reader.ReadLine() + "\n";
}
I am getting an error "Operation not permitted on IsolatedStorageFileStream". Dose it mean my previous session is expired and I cannot see content of the file or something else?
Regards
RE: Read from txt file in a project
posted by: winphonegeek on 5/5/2011 8:59:39 PM
As stated in this MSDN article, data in isolated storage does not persist after the emulator closes. This means that if you run your application and write something in isolated storage, then restart the emulator, what you have written previously in isolated storage will no longer be there.
RE: Read from txt file in a project
posted by: zain on 6/16/2011 10:41:00 AM
is there any way out? is this only for emulator or it will work fine in WP7?
Problems =(
posted by: Paul on 6/27/2011 8:04:27 PM
Unable to determine application identity of the caller.
Where to save text file inside solution?
posted by: lpvoba on 9/17/2011 7:22:26 AM
Just found this post and it's very helpful! Quick clarifying question: in relation to the other files in the solution, are can you save the text files or directory of text files within the solution? Thanks!
IsolatedStorage Cleared only in Emulator
posted by: Alani on 12/16/2011 10:34:38 AM
So, as all the other guys here, the contents of the isolated storage get reset when the emulator is shutdown. Is that the case in the actual phone? I mean, does restarting the app erases the contents of isolated storage in a windows phone?
IsolatedStorage Cleared only in Emulator
posted by: mohamad dekmak on 1/12/2012 2:34:08 AM
please answer the previous question!!
"So, as all the other guys here, the contents of the isolated storage get reset when the emulator is shutdown. Is that the case in the actual phone? I mean, does restarting the app erases the contents of isolated storage in a windows phone?"
RE: IsolatedStorage Cleared only in Emulator
posted by: winphonegeek on 1/12/2012 10:31:20 AM
On an actual Windows Phone device:
- Restarting the app does not erase the contents of its isolated storage.
- Even when your app is updated, its isolated storage is not touched by the system.
- Only when your app is uninstalled its isolated storage is deleted.
See this MSDN article for more information: http://msdn.microsoft.com/en-us/library/ff769544(v=vs.92).aspx
Can i read a .txt file in a textblock ?
posted by: alex on 2/15/2012 5:19:31 PM
I can`t read a text file using Issolated Storage. Any solutions?
thanks !
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
