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
Nice article. Exactly what I need.
Thx.
nice article . thanks
Thanks !
I believe the extension of the filename to be saved is really important as well. I have tried .txt for quite several times and not success, until I have changed it to bare bone “data”; no extension then it hits me.
not success? any error prompt?
i tried it with .txt file it really work fine.
Great article.
Thank U… Simple one
Hi Steve,
Thanks for your article.
I’ve created a small, free and opensource tool to explore IsolatedStorage without installing anything on the phone. Preview of text and image file are possible and you can also explore SqlCE database. Hope it’s help for developpment with Isolated Storage 🙂
http://isostorespy.codeplex.com/
how to store more than one users name??
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you! However,
how could we communicate?
Hi my loved one! I wish to say that this article is amazing, nice written and come with almost all vital infos.
I’d like to peer extra posts like this .
Pingback: Saving Data that never Changes Windows Phone