locked
[UWP][C#] Getting "file not found" with File.DeleteAsync even though file is being deleted RRS feed

  • Question

  • I am writing a UWP app in C# that needs to delete all files in a local file directory that the app created and populated with files. The File.DeleteAsync() method deletes the files but generates a FileNotFound exception with each call to delete a file. I was wondering if the error is because the StorageFile struct is maybe invalid upon return from the call? I changed the codet to ignore the FileNotFound exception and it does eliminate all of the files in the directory. Here is the code:

        try
        {
            StorageFolder imageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("ImageFiles");
            List<string> fileTypeFilter = new List<string>();
            fileTypeFilter.Add("*");
            QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);
            queryOptions.UserSearchFilter = ".png";
            var query = imageFolder.CreateFileQueryWithOptions(queryOptions);
            IReadOnlyList<StorageFile> files = await query.GetFilesAsync();
    
            foreach (StorageFile file in files)
            {
                 try
                 {
                     await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
                 }
                 catch (Exception ex)
                 { }
            }
        }
        catch (Exception ex)
        {
        }
    Thanks Dave


    • Edited by Breeze Liu Friday, May 12, 2017 5:20 AM add [UWP][C#]
    Thursday, May 11, 2017 2:48 AM

All replies

  • Hi OnTheBeach2,

    Welcome to the Developing Universal Windows apps forum! Please utilize tagging when posting to this forum, thanks!

    If the ImageFiles folder does not exist in the local folder, The system cannot find the folder specified, so you will get the exception. In order to give a better user experience, we should catch and handle the exception such as notifying the user the folder is not existed but not ignore the exception.

    Best regards,

    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 12, 2017 6:15 AM
  • The folder exists and the files exist in the folder. That is why I posted the question. The files are deleted one at a time. And I get a FileNotFound exception after the file is deleted with the DeleteAsync request. So to be clear if I look at the directory as I step through each DeleteAsync request, the folder and file exist before the request, the file is no longer there after stepping thru the DeleteAsync request but the exception is generated upon exit from the DeleteAsync request.
    Friday, May 12, 2017 1:21 PM
  • Hi OnTheBeach2,

    Ok, the prerequisite is the folder and the files exist. But I use your above code in my project, it didn't cause any exception whatever the files in the folder exist or not. if the files list is null, it won't execute

    await file.DeleteAsync(StorageDeleteOption.PermanentDelete) code in my side.
    I test it on the OS version 1703, OS Build 15063.296. Please try to test it again or test it on other devices. 
    If the issue still happens, could you give me your OS version and OS build? If possible, could your help me to reproduce it, so that we can investigate and resolve it.

    Best regards,
    Breeze


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Breeze Liu Thursday, May 18, 2017 10:26 AM
    Thursday, May 18, 2017 10:22 AM