locked
StorageFolder->DeleteAsync fails if folder contains recently added file

    Question

  • I am writing unit tests for my code and one test involves deleting a non-empty folder.  Basically the test sequence is this:

    CreateFolderAsync wrapped in a task to make it synchronous

    CopyFile2() // to get the file from test area into folder to be deleted

    DeleteAsync()

    I have the root folder open and watch as I step over the code, and when the delete call is made the folder dispappears.  However, if I hit F5 on in the explorer window the folder shows back up.  If I try to double click to open it, it fails to open.

    I have modified the test and removed the DeleteAsync call and ran it to create all the files.  Then I modified the code to ONLY do the delete, and this time it succeeded.  So, it appears to be a timing issue and that CopyFile2 may not really be done when it returns.  I see nothing in the documentation that indicates the function is asynchronous. http://msdn.microsoft.com/en-us/library/windows/apps/hh449404.aspx

    Can you shed some light on this?

    Regards, Guy

    Wednesday, June 27, 2012 4:12 PM

Answers

  • The CopyFile2 function may be done but something else could have the file open. Creating a new file can trigger other processes to open the file. Anti-virus apps often do this. If the folder is open in Explorer then Explorer may open it to create a thumbnail, etc.

    --Rob

    • Marked as answer by gnichola Friday, June 29, 2012 5:34 AM
    Thursday, June 28, 2012 2:41 AM
    Owner