1

Wondering why "DeleteAsync" doesn't delete file but "File.Delete" will do it. Can someone explain this to me? At first I think that the file is open but if the file is open "File.Delete" shouldn't delete it also or...?

private static async void FILESYSTEM_RemoveVideoPosterIfExist(string posterFileNameOnStorage)
{
    IStorageItem videoPosterIStorageItem = await ApplicationData.Current.LocalFolder.TryGetItemAsync(SYSTEM_UserVideoPosterFolder + @"\" + DATABASE_SelectedUserInformation.UserName + "." + SYSTEM_UserPosterFolderExtension + @"\" + posterFileNameOnStorage);
    if (videoPosterIStorageItem != null)
    {
        try
        {
            //Why this doesn't delete file...
            await videoPosterIStorageItem.DeleteAsync(StorageDeleteOption.PermanentDelete);
        }
        catch
        {
            //But this one will delete file.
            StorageFolder applicationStorageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(SYSTEM_UserVideoPosterFolder + @"\" + DATABASE_SelectedUserInformation.UserName + "." + SYSTEM_UserPosterFolderExtension + @"\");
            File.Delete(applicationStorageFolder.Path + @"\" + posterFileNameOnStorage);
        }
    }
}
Weissu
  • 409
  • 3
  • 15
  • 2
    If it's throwing an exception you need to look at the exception in order to find out why it isn't working. Also, unless this is an event handler, you shouldn't be using `async void`. – Ant P Dec 03 '16 at 12:27

1 Answers1

3

The reason is likely to be that there is no native function to delete a file asynchronously. The managed APIs generally are wrappers around the unmanaged ones.

Take a look at this

Why isn't there an asynchronous file delete in .net?

 FileInfo fi = new FileInfo(fileName);
 await fi.DeleteAsync(); // C# 5
 fi.DeleteAsync().Wait(); // C# 4

Hope this helps!!

Community
  • 1
  • 1
Ryu
  • 191
  • 10