c# - windows phone 7 isolated storage - operation not permitted -
so i've been working on simple game , wanted implement highscore system. once player loads main page first time new text file created ("hsc.txt") , fake values inserted later on split program, however, code throws system.io.isolatedstorage.isolatedstorageexception , can't seem find problem. i've looked error got message box "- operation not permitted" solutions posted don't seem work. have tried closing streams doesn't seem work.
any advice highly appreciated.
private void hashighscores() { string filename = "hsc.txt"; using (var isostorage = isolatedstoragefile.getuserstoreforapplication()) { if (!isostorage.fileexists(filename)) { isostorage.createfile(filename); using (var isostream = new isolatedstoragefilestream(filename, filemode.append, fileaccess.write, isostorage)) { using (var filestream = new streamwriter(isostream)) { filestream.writeline("n1:666,n2:777,n3:888,h1:666,h2:777,h3:888"); filestream.close(); } isostream.close(); } } } } so far have: a) changed filemode b) changed fileaccess , few other "quickfixes" don't remember.
the createfile method returns stream created file, , keeps open. therefore, when try open stream same file in next line, throws exception because file locked.
you can rewrite code follows:
private void hashighscores() { string filename = "hsc.txt"; using (var isostorage = isolatedstoragefile.getuserstoreforapplication()) { if (!isostorage.fileexists(filename)) { using (var isostream = isostorage.createfile(filename)) { using (var filestream = new streamwriter(isostream)) { filestream.writeline("n1:666,n2:777,n3:888,h1:666,h2:777,h3:888"); } } } } } i've removed stream.close() instructions. close method automatically called when enclose stream in using statement.
Comments
Post a Comment