locked
encountered error in deleting file with this error msg RRS feed

  • Question

  • Encountered below error msg when trying to delete a file.

    Additional information: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

    Appreciate your help for this.

    private async void btnDelFile_Click(object sender, RoutedEventArgs e)
            {
               
                bool result = await GetIfFileExistsAsyncDBInfo("myfile");

                if (result == true)
                {
                    var messageDialog = new MessageDialog("Do you want to delete it now?", "Deleting File");

                    messageDialog.Commands.Add(new UICommand("Yes", delegate(IUICommand command)
                    {
                        DeleteFile();

                    }));


                    messageDialog.Commands.Add(new UICommand("No", delegate(IUICommand command)
                    {
                        return;
                    }));

                 
                    await messageDialog.ShowAsync();

                }
                else
                {
                    MessageDialog mError = new MessageDialog("No such file Created", "NO file Created");
                    await mError.ShowAsync();
                }           
            }


            private async void DeleteFile()
            {
              
                StorageFile dbfile = await ApplicationData.Current.LocalFolder.GetFileAsync("myfile");

                if (dbfile != null)
                {
                    await dbfile.DeleteAsync();

                }

      //#####   Encountered error here :  ##########

                StorageFile filefound = await ApplicationData.Current.LocalFolder.GetFileAsync("myfile");

                if (filefound != null)
                {
                    MessageDialog mError = new MessageDialog("This file has NOT been deleted", "File Not deleted");
                    await mError.ShowAsync();
                }
                else
                {
                    MessageDialog mError = new MessageDialog("This file has been deleted", "File deleted");
                    await mError.ShowAsync(); 
                }
            }

     

    private async Task<bool> GetIfFileExistsAsyncDBInfo(string fileName)
            {
                try
                {
                
            StorageFile dbfile = await ApplicationData.Current.LocalFolder.GetFileAsync("myfile");
              
                    //--- Get info
                    strDbFileType = dbfile.FileType.ToString();
                    DBCreationDate = dbfile.DateCreated.DateTime;

                    if (dbfile != null)
                    { 
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (FileNotFoundException)
                {
                    return false;
                    // return default(StorageFile);
                }
            }

     

     

    Wednesday, December 18, 2013 10:01 AM

Answers

  • This is expected behavior: GetFileAsync() throws exception when requested file not found. If you don't want exceptions use TryGetItemAsync() instead.

    Blog: http://lunarfrog.com/blog

    Wednesday, December 18, 2013 6:02 PM