3

I am trying to store a file to the app's storage in a Windows Phone 8.1 app, but it always fails with a System.UnauthorizedAccessException.

The exception occurs on the following line (no related code before that):

StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("test.xml", CreationCollisionOption.ReplaceExisting);
rndsum
  • 81
  • 7
  • At what moment do you try to create the file? – Ulugbek Umirov Apr 15 '14 at 18:15
  • I am doing this in the OnNavigatedTo method of the page. – rndsum Apr 15 '14 at 18:17
  • Are you awaiting on this line? Or you just call it as you have written? – Romasz Apr 15 '14 at 18:21
  • Yes, I am. I updated the code snippet I posted above to represent the entire line of code I am using. – rndsum Apr 15 '14 at 18:45
  • As it is run async, check if something else is not trying to use the same file. Also try to set a breakpoint at that line (or line before) and step over - see what happens. – Romasz Apr 15 '14 at 19:28
  • Nothing else inside my app is using that file, I only have this one line of code that uses "test.xml". Setting a breakpoint on that line I see it throwing the exception shortly after I step over it (F10). – rndsum Apr 15 '14 at 22:51
  • I've tried this line and it's working fine. The main question is - what kind of project are you trying to build? WP8 or WP8.1? – Romasz Apr 16 '14 at 06:10
  • WP8.1, which one did you use? I literally created a new project of type "Blank App (Windows Phone)", added the above line to the MainPage's OnNavigatedTo stub and changed that method to be async, nothing else. – rndsum Apr 16 '14 at 17:01
  • What emulator or device are you using to run the code in? Have you tried replacing ReplaceExisting with another option? – Adam Apr 17 '14 at 16:11
  • I am using the Windows Phone 8.1 emulator. I uninstalled Visual Studio and re-installed Visual Studio 2013 Update 2 RC and re-installed the emulator from within Visual Studio, still no luck, same exception. The exception is no longer present in the clean new project, not sure how it disappeared. I got it to go away in my project for a short duration, too, after I added and removed the 'local storage' capability. When I played around with it for a while it came back though. :\ – rndsum Apr 17 '14 at 22:00
  • I just tried your suggestion and changed it from ReplaceExisting to GenerateUniqueName. This made it work, now I get the UnauthorizedAccessException when reading that file. – rndsum Apr 17 '14 at 22:02
  • What is the 'local storage' capability you are using? I don't see one called local storage listed here: http://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx – Adam Apr 18 '14 at 15:53
  • Sorry, I mistyped that. I was enabling/disabling the "Removable Storage" capability. I actually shared the code with a friend who ran it on his phone and he is not getting this exception with the exact same code. :/ – rndsum Apr 19 '14 at 00:19
  • It is working again at the moment. I was calling CreateFileAsync from a static method before. Changing that method to be non-static fixed this. Any idea what the reason for this is? – rndsum Apr 19 '14 at 20:30

1 Answers1

4

Finally found the issue:

I had a call to

Windows.Storage.ApplicationData.Current.LocalFolder.DeleteAsync()

in code that ran once after installing the app. Therefore all writing to this folder failed after that point.

I am not sure why GenerateUniqueName made the UnauthorizedAccessException disappear before, but since I wasn't able to read the file when using GenerateUniqueName I assume that specific code path doesn't ever throw that exception for some reason, maybe because it assumes there can never be an issue when using unique file names.

rndsum
  • 81
  • 7