LocalFolder.CreateFolderAsync() and System.UnauthorizedAccessException


  • The following code:

    StorageFolder sf = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Temp", CreationCollisionOption.ReplaceExisting);

    works most of the time, but it generates the following exception sometimes:

    System.UnauthorizedAccessException was caught
      Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
           at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
           at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
           at Myapp.MainPage.<HandleFile>d__18.MoveNext()

    This tends (not always) to happen when the app is opened by double-clicking a supported file.

    I am wondering if anyone could offer some tips on how to debug this.


    Tuesday, February 24, 2015 4:31 AM

All replies

  • I think you could change the second param to CreationCollisionOption.OpenIfExist

    The error could be that you cann't replace temp folder which already exists.

    StorageFolder sf=null;
    sf=await ApplicationData.Current.LocalFolder.CreateFolderAsync("Temp", CreationCollisionOption.OpenIfExists);
    catch (Exception)
    //too bad, have to leave with it

    Tuesday, February 24, 2015 6:39 AM
  • It's probally because the file is already open somewhere else. dont forget to close streams

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, February 24, 2015 8:08 AM
  • Thank you all for the replies.

    Dave, I was also thinking in that line, but it cannot explain how it is reproduced as following:

    1. Click "Stop debugging" to stop the app completely.
    2. Check Task Manager to make sure no instance of the app is running.
    3. Launch the app from VS without starting it.
    4. Double-click an image file to start the app.
    5. The app breaks at the aforementioned CreateFolderAsync() statement.
    6. Step over the statement, and the exception is generated.

    This is only one of the few ways to reproduce this.  Again, it works fine most of the time.

    The only statement executed prior to this one is the following:

    StorageFile _sfSelected = await (await ApplicationData.Current.LocalFolder.GetFolderAsync("Temp")).GetFileAsync(sFileName);
    In this particular case, the above statement always generates System.IO.FileNotFoundException regardless of whether System.UnauthorizedAccessException will be generated by the statement in question. FileNotFoundException happens before UnauthorizedAccessException.  No stream reading or writing is involved at the time of UnauthorizedAccessException.


    Tuesday, February 24, 2015 2:28 PM
  • First , if you want to attach a debugger


    Read "Activations"

    Print out the temp folder full path it must have existed.

    The first await in your code assign it to a variable and dispose it after use.

    Still, see my first post for a softer get temp if it does not yet exist, create one approach.

    You will otherwise run into different problems

    • Edited by hereafter Wednesday, February 25, 2015 12:21 AM
    Wednesday, February 25, 2015 12:20 AM
  • My intention is deleting the contents of the folder if it exists.  This is why I use CreationCollisionOption.ReplaceExisting intentionally.

    The debugger has been working exactly as expected. There is no issue with the debugger.  I am sorry if my previous post confused you.


    Wednesday, February 25, 2015 12:30 AM
  • what have you tried since then? !!!
    Wednesday, February 25, 2015 2:33 AM
  • I catch the exception, wait briefly (200 ms), then execute the statement again. It has worked fine so far. I still wish to understand why the exception is generated.


    Wednesday, February 25, 2015 2:44 AM