c# - Strange issue with IsolatedStorage in WP7 device -
i have strange problem in windows phone 7 application. need read/write xml file in app , i'm using isolatedstorage
collect data. app sends/gets data skydrive
why use it.
ok, here function generate exception:
private void createfileintoisolatedstorage(list<record> list) { isf = isolatedstoragefile.getuserstoreforapplication(); if(list.count == 0) list = new list<record>() { new record { date = datetime.today, value = 0 }}; if (isf.fileexists(filename)) { isf.deletefile(filename); } xmlwritersettings xmlwritersettings = new xmlwritersettings(); xmlwritersettings.indent = true; using (isolatedstoragefile myisolatedstorage = isolatedstoragefile.getuserstoreforapplication()) { using (isolatedstoragefilestream stream = myisolatedstorage.openfile(filename, filemode.create)) { xmlserializer serializer = new xmlserializer(typeof(list<record>)); using (xmlwriter xmlwriter = xmlwriter.create(stream, xmlwritersettings)) { serializer.serialize(xmlwriter, list); } } } }
problem:
my problem starts when function runs second time. isf.deletefile(filename);
throws isolatedstorageexception. , creating stream
crashed application.
it's strange cause happens every time run on device, , when use debugger.
so question how can solve or there better ways this?
any appreciated.
possibly it's because @ beginning of method have:
isf = isolatedstoragefile.getuserstoreforapplication();
and never dispose of that. then, later, again in using
. one's disposed. , next time call createfileintoisolatedstorage
, again, again without disposing.
perhaps want:
using (var isf = isolatedstoragefile.getuserstoreforapplication()) { if(list.count == 0) list = new list<record>() { new record { date = datetime.today, value = 0 }}; if (isf.fileexists(filename)) { isf.deletefile(filename); } }
although class-scoped isf
variable troublesome. if want keep store active, call once , leave open. otherwise, ditch class-scoped variable.
or, might due this, documentation isolatedstoragefile.deletefile?
file deletions subject intermittent failures because files can in use simultaneously operating system features such virus scanners , file indexers. true created files. macintosh users should aware of issue because of frequent indexing. these reasons, important add code code block handles isolatedstorageexception retry deleting file or log failure.
i suggest like:
int retrycount = 0; while (retrycount < maxretrycount && isf.fileexists(filename)) { try { isf.deletefile(filename); } catch (isolatedstorageexception) { ++retrycount; // maybe notify user , delay briefly // or forget retry , log error. let user try again. } }
Comments
Post a Comment