Windows Runtime: Different behavior of StorageFolder.DeleteAsync when deleting from different locations

2

I am a bit confused about the behavior of DeleteAsync when invoking it on StorageFolder objects representing folders in different parent locations. If the parent location of the folder I want to delete is for example the local application data folder, I can delete folders even if they have content (files and subfolders). If the parent location is for example the documents library, I get an Exception "The directory is not empty. (Exception from HRESULT: 0x80070091)" if the folder is not empty.

I of course declared the capability to access the documents library and declared the file types in the app manifest. To proof that accessing these files (text files in my test case) works my test app creates the folder and creates text files in it. The app is even allowed to delete single text files from the folder. But it is not allowed to delete the whole folder if the parent folder is the documents library.

Can someone explain this different behavior? It may have to do with the assumption (I don't know for sure) that data in the application data folders is handled more relaxed than other data (which most likely is user data). A hint for this assumption is that deleting files in the application data folders always deletes them permanently, but deleting in other folders allows moving to the recycle bin.

Edit: Here is my test app: www.juergen-bayer.net/Downloads/FolderDeletionTest.zip

windows-runtime
windows-store-apps
asked on Stack Overflow Oct 17, 2012 by Jürgen Bayer • edited Jan 6, 2013 by Jürgen Bayer

2 Answers

1

The behaviour you describe seems perfectly sensible to me. The application local folder is owned by your app, and theoretically all it's contents are available to your app to work with as it pleases. Nested folder deletion is a good case in point. Your app should be able to delete all its own data as it sees fit, including removing non-empty folders.

The user's documents folder, however, is a very different beast. There is nothing stopping (or even making it difficult for) your user to add content to a folder you created with your app. Just opening explorer will allow them access to the content of the folder. If your app wants to delete a folder here, you have to be absolutely certain there are no user created documents in the folder before you delete it. Adding this additional hurdle when deleting in code should make the developer think about the action they are about to take, if not actually preventing them deleting contents one by one.

So, in short: your app owns its own folders, and can be as cavalier as it likes about its own data. It doesn't own the documents library, and so you should not have the ability to delete whole folders full of files at a single stroke.

answered on Stack Overflow Jan 6, 2013 by ZombieSheep
0

It seems that some files or directories in this folder is accesed at this time by other thread/application or even your code. try to delete files one by one I think you will recieve access error.

answered on Stack Overflow Jan 5, 2013 by Gaploid

User contributions licensed under CC BY-SA 3.0