c# - Cannot access LocalFolder files on Windows phone app (UnauthorizedAccessException) -
i trying application write (then read) simple text file in windows phone 8. app has 3 controls: create file button, display file button, , textbox contents supposed display.
i have following events set in code:
private async void btncreatefile_click(object sender, routedeventargs e) { await applicationdata.current.localfolder.createfileasync("myfile.txt"); storagefile file = await applicationdata.current.localfolder.getfileasync("myfile.txt"); streamwriter writer = new streamwriter(await file.openstreamforwriteasync()); writer.writeline("hello world"); writer.writeline("goodbye world"); } private async void btnshowfile_click(object sender, routedeventargs e) { storagefile file = await applicationdata.current.localfolder.getfileasync("myfile.txt"); streamreader reader = new streamreader(await file.openstreamforreadasync()); string text = reader.readtoend(); text1.text = text; } }
the application throwing unauthorizedaccessexception when streamreader being created. can shed light on why?
i guess you're not disposing streamwriter
. see example on msdn.
using( var writer = new streamwriter(await file.openstreamforwriteasync())) { writer.writeline("hello world"); writer.writeline("goodbye world"); }
that's why can't read file, because it's taken sreamwriter.
Comments
Post a Comment