locked
StorageFile.DeleteAsync() delete file fail without any exception sometimes.

    Question

  • I develop a test tool to Write a file, then delete it, over and over again.

    Using "await StorageFile.DeleteAsync()" to delete file , sometimes, it run successfully without any exception, but the file to be deleted wasn't deleted as expect. Why the API " StorageFile.DeleteAsync" doesn't work?

    Thanks.

    Tuesday, January 06, 2015 7:49 AM

All replies

  • do you have a sample project? i think it should always throw an exception when it fails to delete the file. can the difference be that sometimes you run the code from a other task?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, January 06, 2015 8:29 AM
  • Hello,

    Code is as this:

     private async void SDCardFile(String basename)
            {
                while (true)
                {
                   
                   ...
                    if (this.isStop)
                    {                  
                        return;
                    }
                    Boolean isWirteSuc = await WriteSDCardFile(fileName, writeText);//writefile
                    if (isWirteSuc)
                    { ...
                    }
                    else
                    { ...
                    }
                   
                    Boolean isDeleteSuc = await DeleteSDCardFile(fileName);  //deletefile
                    if (isDeleteSuc )
                    { ...
                    }
                    else
                    { ...
                    }
                    
                }            
            }

     private async Task<bool> DeleteSDCardFile(String filename)
            {
                try
                {
                    StorageFolder externalDevices = KnownFolders.RemovableDevices;
                    StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();
                    StorageFile storageFile = await sdCard.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
                    await storageFile.DeleteAsync();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }

            }

    I use a timer to stop the test after 12 hours, during this test, most of the file delete success, but some failed, it still return "true"  from "DeleteSDCardFile(String filename)", so there is no exception, but the file is not deleted, I can find these files on SD card.

    Look forward to your answer, thanks for your comments.

    Tuesday, January 06, 2015 9:33 AM
  • Hi,

    I cannot reproduce the problem. When I test in my computer, all of files can delete. Would you mind sharing a reproduce sample to OneDrvie. And you say you cannot delete some files, you'd better post the file to OneDrive so I can test your project with them.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, February 06, 2015 6:00 AM
  • Hello Anne,

    This problem doesn't happen each time, it is a stress test. It continously write file, read, then delete. And after 2s, do the operation (write & read & delete) again and again. In 12 hours test,there may be about 10 times that the files wasn't deleted successfully.

    It is a windows phone runtime 8.1 application that run on windows phone, and operate sd card.

    I don't know why it happened, may be something related to the operation path is sd card? There was no exception thrown, using Unhandled Exception to catch it also didn't work.  Do you have any ideas? Thanks.

    And sorry that the app can't be uploaded. It is a enterprise application.

    Thanks a lot.

    Friday, February 06, 2015 7:36 AM