locked
Delete a file, file list doesn't reflect this?

    Question

  • This is a universal app.

    I'm deleting and reloading a file list in one function. But I'm having trouble getting the file list to reflect the deletion of a file.

    I have a variable theFolder that's a StorageFolder available to the whole class.

    Here's the sequence of events:

    StorageFile f = await theFolder.GetFileAsync(title);
    await f.DeleteAsync();
    theFolder = //Make this equal to the same folder as last time

    The problem is I soon call a theFolder.GetFilesAsync() to work with a file list and that file list still contains the deleted file. Looking at the folder, the file is indeed deleted, but GetFilesAsync() does not reflect this change for some time. At first, I thought that after the deletion, I needed to "refresh" theFolder by making it equal the folder again, hence the final line in the code block. I'm not entirely sure if that's needed though.

    Thoughts?


    • Edited by EntegyMVP Friday, October 31, 2014 2:23 AM
    Friday, October 31, 2014 2:20 AM

Answers

  • The way it works is confusing.  You can see this for an explanation:
    http://msdn.microsoft.com/EN-US/library/vstudio/hh191443(v=vs.110).aspx#BKMK_WhatHappensUnderstandinganAsyncMethod

    This should solve the problem:

    StorageFile f = await theFolder.GetFileAsync(title);
    var DeleteMethod = f.DeleteAsync();
    DeleteMethod.Completed = (async (x, y) =>
        {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async ()=>  
          {
           // Do stuff on UI thread to show the folder contents
          });
        });



    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.

    Wednesday, November 19, 2014 7:42 PM
    Moderator

All replies

  • It may be because the file list is being refreshed before the deletion is complete. Try this:

    Have two buttons, one which performs the file deletion, one which updates the list. Click the delete button first, then the refresh button.  Does the list refresh properly, or stay the same?

    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.

    Friday, October 31, 2014 1:24 PM
    Moderator
  • Yes, the list refreshes properly. So the issue is that I'm calling for a refresh of the file list too quickly? I was under the impression that by awaiting DeleteASync(), the function wouldn't return until the file deletion is completed.

    It's clear I'm misunderstanding though. How can I properly get a refreshed file list?

    Saturday, November 1, 2014 12:41 AM
  • The way it works is confusing.  You can see this for an explanation:
    http://msdn.microsoft.com/EN-US/library/vstudio/hh191443(v=vs.110).aspx#BKMK_WhatHappensUnderstandinganAsyncMethod

    This should solve the problem:

    StorageFile f = await theFolder.GetFileAsync(title);
    var DeleteMethod = f.DeleteAsync();
    DeleteMethod.Completed = (async (x, y) =>
        {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async ()=>  
          {
           // Do stuff on UI thread to show the folder contents
          });
        });



    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.

    Wednesday, November 19, 2014 7:42 PM
    Moderator