Answered by:
IsolatedStorageFile could not be found.

Question
-
This code stopped working in Windows Phone 8.1 then I came to know that WinRT apps removed IsolatedStorageFile.
Please someone help me to find Workaround
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); if (iso.FileExists("AccountArchive.xml")) { XmlSerializer xs = new XmlSerializer(typeof(AccountsList)); using (sr = new StreamReader(iso.OpenFile("AccountArchive.xml", FileMode.Open))) { this.Database = (AccountsList)xs.Deserialize(sr); } } using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) { var xs = new XmlSerializer(typeof(AccountsList)); var stream = iso.OpenFile("AccountArchive.xml", FileMode.Create); using (var sw = new StreamWriter(stream)) { xs.Serialize(sw, this.Database); sw.Flush(); } }
Friday, July 25, 2014 9:00 AM
Answers
-
well some feedback;
- the first run your xml file isn't exciting yet. You need to handle that scenario too
- at this moment you never call DoAuth method so the file wont get loaded
- you OnSuspending isn't right yet here an example
private async void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("AccountArchive.xml", CreationCollisionOption.ReplaceExisting); using (var iso = await file.OpenStreamForWriteAsync()) // MY CODE ------------------------------------------------------------------ { var xs = new XmlSerializer(typeof(AccountsList)); using (var sw = new StreamWriter(iso)) { xs.Serialize(sw, this.Database); await sw.FlushAsync(); } } deferral.Complete(); }
make notice its important to call deferral.COmplete() at the end.Microsoft Certified Solutions Developer - Windows Store Apps Using C#
- Marked as answer by xSHERU Sunday, July 27, 2014 10:35 PM
Friday, July 25, 2014 12:32 PM -
Hi Dave,
Thanks for the help now I have get all the things working.
But still if you can see and tell me if I can improve something anywhere will be great......
Updated Code:
https://onedrive.live.com/redir?resid=753EEF01C5C097CD%2122362 (Sample App)
public async void CheckFileAvail() { bool FileAvail = await DoesFileExist("AccountArchive.xml"); if (FileAvail == true) { DoAuth(); } else { } } public async Task<bool> DoesFileExist(string FileName) { try { await ApplicationData.Current.LocalFolder.GetFileAsync(FileName); return true; } catch { return false; } } private async void DoAuth() { try { File = await ApplicationData.Current.LocalFolder.GetFileAsync("AccountArchive.xml"); FileStream = await File.OpenStreamForReadAsync(); XmlSerializer XS = new XmlSerializer(typeof(AccountsList)); using (FileStreamReader = new StreamReader(FileStream)) { this.Database = (AccountsList)XS.Deserialize(FileStreamReader); } } catch { DeleteAuth(); //File is Corrupted } } private async void DeleteAuth() { DataCorruptionException = true; if (FileStreamReader != null) { FileStreamReader.Dispose(); } File = await ApplicationData.Current.LocalFolder.GetFileAsync("AccountArchive.xml"); if (File != null) { await File.DeleteAsync(); } }
- Marked as answer by xSHERU Sunday, July 27, 2014 10:35 PM
Friday, July 25, 2014 10:18 PM
All replies
-
you should use class in the namespace WIndows.Storage
For local files:
ApplicationData.Current.LocalFolder were you can create files
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Friday, July 25, 2014 9:26 AM -
Thank for Quick Reply...
I have tried this way its not giving me error while build but when I deploy it on device its giving me
A first chance exception of type 'System.NullReferenceException' occurred
A first chance exception of type 'System.Exception' occurred
private async void DoAuth() { try { StorageFile File = await ApplicationData.Current.LocalFolder.GetFileAsync("AccountArchive.xml"); Stream FileStream = await File.OpenStreamForReadAsync(); XmlSerializer XS = new XmlSerializer(typeof(AccountsList)); using (StreamReader FileStreamReader = new StreamReader(FileStream)) { this.Database = (AccountsList)XS.Deserialize(FileStreamReader); } } catch { // } }
- Edited by xSHERU Friday, July 25, 2014 9:42 AM
Friday, July 25, 2014 9:41 AM -
is the file already existing? do you have a sample project that you can share? then can help better
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Friday, July 25, 2014 9:48 AM -
Give me some time I am creating SampleFriday, July 25, 2014 9:56 AM
-
oke.. I have an alert on the thread so will get a mail when you posted a link
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Friday, July 25, 2014 10:00 AM -
https://onedrive.live.com/redir?resid=753EEF01C5C097CD%2121533
Please Read Text File in Project
Friday, July 25, 2014 10:25 AM -
well some feedback;
- the first run your xml file isn't exciting yet. You need to handle that scenario too
- at this moment you never call DoAuth method so the file wont get loaded
- you OnSuspending isn't right yet here an example
private async void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("AccountArchive.xml", CreationCollisionOption.ReplaceExisting); using (var iso = await file.OpenStreamForWriteAsync()) // MY CODE ------------------------------------------------------------------ { var xs = new XmlSerializer(typeof(AccountsList)); using (var sw = new StreamWriter(iso)) { xs.Serialize(sw, this.Database); await sw.FlushAsync(); } } deferral.Complete(); }
make notice its important to call deferral.COmplete() at the end.Microsoft Certified Solutions Developer - Windows Store Apps Using C#
- Marked as answer by xSHERU Sunday, July 27, 2014 10:35 PM
Friday, July 25, 2014 12:32 PM -
Hi Dave,
Thanks for the help now I have get all the things working.
But still if you can see and tell me if I can improve something anywhere will be great......
Updated Code:
https://onedrive.live.com/redir?resid=753EEF01C5C097CD%2122362 (Sample App)
public async void CheckFileAvail() { bool FileAvail = await DoesFileExist("AccountArchive.xml"); if (FileAvail == true) { DoAuth(); } else { } } public async Task<bool> DoesFileExist(string FileName) { try { await ApplicationData.Current.LocalFolder.GetFileAsync(FileName); return true; } catch { return false; } } private async void DoAuth() { try { File = await ApplicationData.Current.LocalFolder.GetFileAsync("AccountArchive.xml"); FileStream = await File.OpenStreamForReadAsync(); XmlSerializer XS = new XmlSerializer(typeof(AccountsList)); using (FileStreamReader = new StreamReader(FileStream)) { this.Database = (AccountsList)XS.Deserialize(FileStreamReader); } } catch { DeleteAuth(); //File is Corrupted } } private async void DeleteAuth() { DataCorruptionException = true; if (FileStreamReader != null) { FileStreamReader.Dispose(); } File = await ApplicationData.Current.LocalFolder.GetFileAsync("AccountArchive.xml"); if (File != null) { await File.DeleteAsync(); } }
- Marked as answer by xSHERU Sunday, July 27, 2014 10:35 PM
Friday, July 25, 2014 10:18 PM