Windows Phone 8 shared core with Windows 8 – File IO
For developers, the biggest news for Windows Phone 8 is that it shares a core with Windows 8. It means that your code has more chances than ever before to be compatible on both platforms.
One of the easier components and surely the most used in applications is the File IO component. In this post, I’ll also cover the Windows Phone 7 code compatibility.
In Windows Phone 7
To write a file, you use the IsolatedStorageFile:
private void WriteFile(string fileName, string content) { using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.CreateFile(fileName)) { using (StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream)) { streamWriter.Write(content); } } } }
To read a file:
private string ReadFile(string fileName) { string text; using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.OpenFile(fileName, FileMode.Open)) { using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream)) { text = streamReader.ReadToEnd(); } } } return text; }
In Windows Phone 8
To write a file, you use the Windows.Storage component. The file and folder objects are respectively IStorageFile and IStorageFolder. The usage of the await and async keywords come in.
To write a file:
public async Task WriteFile(string fileName, string text) { IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (Stream stream = await storageFile.OpenStreamForWriteAsync()) { byte[] content = Encoding.UTF8.GetBytes(text); await stream.WriteAsync(content, 0, content.Length); } }
To read a file:
public async Task<string> ReadFile(string fileName) { string text; IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName); IRandomAccessStream accessStream = await storageFile.OpenReadAsync(); using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size)) { byte[] content = new byte[stream.Length]; await stream.ReadAsync(content, 0, (int) stream.Length); text = Encoding.UTF8.GetString(content, 0, content.Length); } return text; }
You can use these methods this way:
await WriteFile("Dummy.txt", "I love the Windows Phone"); string text = await ReadFile("Dummy.txt");
You can see that the code between Windows Phone 7 and Windows Phone 8 is quite different.
Compatibility
As we know already, Microsoft announced that the Windows Phone 7 applications will run on Windows Phone 8. This is great news. What is even better is that your Windows Phone 7 code that uses the IsolatedStorage will compile without any changes in Windows Phone 8. You can take the Windows Phone 7 code above and it will work.
You might be wondering if the internal folder structure is different between WP7 and WP8… The difference is minor.
In WP7, if you create a file at the root, the file will point to:
C:DataUsersDefaultAppAccountAppData{9679D8C4-24B4-41DF-85C5-5E099947F109}LocalIsolatedStoreDummy.txt
In WP8, the same file created at the root will be in:
C:DataUsersDefaultAppAccountAppData{9679D8C4-24B4-41DF-85C5-5E099947F109}LocalDummy.txt
The difference is the IsolatedStore folder added in WP7. It means that if you have old WP7 code that you would like to use with new WP8 code, you just have to get the IsolatedStore folder:
IStorageFolder applicationFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("IsolatedStore");
In the end, you can mix both WP7 and WP8 code in your next Windows Phone 8 application. However, I would encourage you to use only the Windows Phone 8 version, as it will then be easier for you to use the same code in Windows 8.
tishawn Fahie
Nov 07, 2012 @ 19:19:02
This was very helpful! Thank you so much for this. Let me ask you suppose I want to save score’s (int) and player name(string). How could I modify this. I’ve tried but failed.
Thank you for reading.
archiecoder
Nov 07, 2012 @ 22:05:03
Hello!
Thank for reaching my website. What I suggest you is making a class that holds the save score and the player name and use the Json.net framework to serialize (and deserialize) an object into a json string then you can use my methods. Json.net is one of the most popular frameworks in the .NET world. It is available in nuget.
To check for the existence of a file, with Windows 8 there is no method! You can do it with a try/catch around opening a file.
Good luck
ArchieCoder
Ashish
Aug 07, 2013 @ 02:34:34
Hi archiecoder,
thanks for valuable post, i have a small question,
Can we serialize (and deserialize) an object using DataContractJsonSerializer ? if we can please show a demo.
archiecoder
Aug 07, 2013 @ 08:56:27
Yes you can use DataContractJsonSerializer, but I prefer to use Json.NET. Take a look at https://dotnetapp.com/blog/2012/11/25/how-to-avoid-application-settings-corruption-when-a-background-agent-is-used/
tishawn Fahie
Nov 07, 2012 @ 19:27:35
Also what if I wanted to check and see if that save file exist?
tishawn Fahie
Nov 08, 2012 @ 19:59:56
Thank you very much!! Will Do I love your blog. Full of information thank you!
Robert Oschler
Apr 05, 2013 @ 12:16:34
Great article Sébastien, Thanks!
archiecoder
Apr 05, 2013 @ 12:36:15
Thank you Robert!
Mohamed Assem
Apr 21, 2013 @ 05:05:40
Thanks a lot you have just helped me to solve Windows 8 app problem