6

i try whithout success to delete a file in my local storage. Exactly, i took a photo and i want to delete it later with a button for exemple. But when i click on the button, the app bugs and i have : "access denied".

I sude a simple Delet.Async() after i get the file in a StorageFile.

    private async void delete_click(object sender, RoutedEventArgs e)
    {

            StorageFile filed = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg");
            if (filed != null)
            {
                await filed.DeleteAsync();

            }


    }
Sw1a
  • 245
  • 4
  • 15

3 Answers3

7

Try the code below to see if it works for you.

    private async void takephoto_click(object sender, RoutedEventArgs e)
    {


        var ui = new CameraCaptureUI();
        ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
        var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);

        if (file != null) 
        {
           // store the file
           var myFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("myImg.jpg");
           await file.MoveAndReplaceAsync(myFile);

           // display the file
           var bitmap = new BitmapImage();
           bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
           Photo.Source = bitmap;
        }



    }

    private async void delete_click(object sender, RoutedEventArgs e)
    {
        StorageFile filed = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg");
        if (filed != null)
        {
            await filed.DeleteAsync();
        }

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

        if (filefound != null)
        {
           // do something here 
        }
    }
Zhiming Xue
  • 352
  • 3
  • 4
  • The trouble with this solution is that the second "GetFileAsync" operation will not return 'null', given the current implementation of WIndows RT. Instead, when 'GetFileAsync' fails to find the requested file, it creates and returns an empty one, so, with this code, you will never be able to confirm deletion of the file in question. A superior solution is to call the awaited 'DeleteAsync' function inside a try/catch block and then handle the exception - an exception would indicate failure of the requested deletion without the buggy and problematic second 'GetFileAsync' call. – StephenDonaldHuffPhD Jun 13 '15 at 15:29
0

i am having same problem in deleting file from local storage. there are many files stored in the local storage with different names so how to delete other files. in the above case you have hard coded the file name to delete.

StorageFile filefound = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg"); instead of myImg.jpg user want to delte other file then how user will delete

0
    /// <summary>
    /// Delete the indicated application file
    /// </summary>
    /// <param name="strFilePathName">The file path name to delete</param>
    /// <returns>True, if successful; else false</returns>
    public async static Task<bool> DeleteAppFile(string strFilePathName)
    {
        try
        {
            StorageFile fDelete = null;

            if (!strFilePathName.Equals(""))
            {
                fDelete = await ApplicationData.Current.LocalFolder.GetFileAsync(strFilePathName);
                if (fDelete != null)
                {
                    try
                    {
                        await fDelete.DeleteAsync();
                    }
                    catch (Exception ex)
                    {
                        AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile {" + strFilePathName + "}", ex.Message);

                        return false;
                    }

                    return true;
                }
            }
            else
                AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile", "File path name is empty.");
        }
        catch (Exception ex)
        {
            AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile {" + strFilePathName + "}", ex.Message);
        }

        return false;
    }
  • Under what conditions may `DeleteAsync` throw an exception? Intellisense doesn't indicate any exceptions may be thrown. Furthermore, if the file exists, is success guaranteed? –  Mar 26 '17 at 02:50
  • It will actually throw `FileNotFoundException` if the file does not exist. I don't think there's any reason to put the `DeleteAsync` call in a `try/catch` because if a `StorageFile` object can be created, it exists. There is nothing on MSDN to suggest an empty `StorageFile` object is returned if the file does not exist; therefore, `if (fDelete != null)` would be sufficient. I don't think access is an issue either because you're automatically granted access to local storage. –  Mar 26 '17 at 02:58
  • The try/catch syntax is habit - there are other statements in the block that may throw exceptions - rather than tailor my code to the rare bit that doesn't, I encapsulate them all. I use too many buggy programs that don't take the time to try() anything. – StephenDonaldHuffPhD Apr 07 '17 at 16:15