locked
file.DeleteAsync() throws exception Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) RRS feed

  • Question

  • I am writing a Windows store app and I am attempting to delete a file that I no longer need from my local storage. The code is straightforward

    StorageFiletempfile = awaitApplicationData.Current.LocalFolder.GetFileAsync("image.png");

    if(tempfile != null)

    {

    awaittempfile.DeleteAsync();

    }

    However, when I try to do this, I get the exception

    Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

    I have looked everywhere to find an answer, but the examples typically only show how to read/write data, not how to delete files. I don't see anything from the api's that would suggest there is anything I need to do with permissions or there is some other call I should be making.

    Monday, February 18, 2013 1:13 AM

Answers

  • It is less likely the fact that the image is on the screen and more likely how it was created and/or opened.

    You can also test this theory by hardcoding the filename in the code to delete an image that wasn't created/opened in the same test session. If it successfully deletes then the theory is correct. If you're unable to delete any file in this folder then it is a permission issue.

    Can you share the following:

    - Your test results from the above scenario

    - Code where you first create the image

    - Code where you open the image

    - How the image is bound to the XAML

    Wednesday, February 20, 2013 2:46 AM

All replies

  • There's nothing wrong with your code - I tested it.  Can you uninstall the app, clean the project, and redeploy?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, February 18, 2013 9:41 PM
    Moderator
  • When is this file written or edited? It is possible that you're trying to delete this file while it is still being used by another thread or stream.

    HTH.

    M.

    Monday, February 18, 2013 10:02 PM
  • It looks like you might be on to something. The image is currently being displayed on a page. The 'delete' button has been pressed and this method it attempting to delete the image and then the db record associated with the data on the page. I am having the same issue when I try to replace the image with another one as the result of an edit operation. So, if that is the case, how can I delete/replace the image?
    Wednesday, February 20, 2013 1:32 AM
  • It is less likely the fact that the image is on the screen and more likely how it was created and/or opened.

    You can also test this theory by hardcoding the filename in the code to delete an image that wasn't created/opened in the same test session. If it successfully deletes then the theory is correct. If you're unable to delete any file in this folder then it is a permission issue.

    Can you share the following:

    - Your test results from the above scenario

    - Code where you first create the image

    - Code where you open the image

    - How the image is bound to the XAML

    Wednesday, February 20, 2013 2:46 AM