Search code examples
windows-phone-8windows-phone-8.1windows-phone-8-emulator

Exception when trying to create a directory on Windows Phone 8


I'm getting an error when I try to create a directory (i.e. snapshots) in my wp8 app but I don't understand why it works fine when in my emulator and when I transfer my app directly from my laptop onto my phone but when it's coming from the wp8 store, it generates the following error:

System.UnauthorizedAccessException: Access to the path 'Snapshots\' is denied.

I don't know if this is ok to do but everywhere I deal with files, I don't bother specifying a path but just a folder as I assume that it will go into the "app path\". Is this not the case?

2 Questions arising from the problem:

  1. Why does it work with local app and not app downloaded from the store? Are access rights different?

  2. If I'm not allowed to create files directly into the app's folder (no path provided, just a filename i.e. data.xml) or sub-folder (sub-folder only + filename provided i.e. Snapshots\test.jpg), where should I store my data (xml, jpg, etc...)

Thanks.


Solution

  • I found out the hard way that you should not try to write data directly in the root folder of a wp8 app as it is an "install folder". I found this interesting article/pdf from Microsoft Files and Storage in Windows Phone 8. Check out page 7, where it clearly explains the difference between installation and local folder.

    The question originally originate trying to figure out why my live tile would not update and there were numerous reasons, so while unrelated to this question, I'll include them anyway as it may help someone in the future.

    1. Folder creation problem: See above. Still can't explain why it works when app is transferred from pc to phone but when coming from the app store, it doesn't. Point is that I was going about it the wrong way and you should not be storing files in the root folder, even in a sub-folder as it can't be created in this location. You should use isolated storage or data storage.

    2. Files still failed to update the live tiles when storing them in StorageFolder (LocalFolder). I then found these 2 articles which clearly state that images used by the Live Tiles should be stored in Isolated Storage but in a specific location i.e. Shared\ShellContent

    WP8 Tip: Creating Live Tiles with Dynamic Images

    Capturing image from camera and updating live tile

    The minute I applied what was suggested i.e. use the specific path and use isolated storage, it sorted all my problems i.e. creating folder, creating file, etc...

    Please note that while it states that you should store the file in \Shared\ShellContent, it also highlights that it can be stored in any sub-folders within that folder, so in my case, I've got \Shared\ShellContent\\Snapshots and it work as expected.

    Again, if time permits I'll investigate the StorageFolder some more as I'd like to get it to work as it is the recommended method to use with WP8/WP8.1 by Microsoft and Isolated Storage appears to be recommended for WP7.

    I hope it will save you some time if you encounter similar problems.