How delete file in localstorage on winrt?

Sw1a picture Sw1a · Feb 20, 2013 · Viewed 9.3k times · Source

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();

            }


    }

Answer

Zhiming Xue picture Zhiming Xue · Feb 22, 2013

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 
        }
    }