Sw1a
Sw1a

Reputation: 245

How delete file in localstorage on winrt?

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

            }


    }

Upvotes: 6

Views: 9514

Answers (3)

StephenDonaldHuffPhD
StephenDonaldHuffPhD

Reputation: 958

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

Upvotes: 0

Inayat Ali Jan
Inayat Ali Jan

Reputation: 81

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

Upvotes: 0

Zhiming Xue
Zhiming Xue

Reputation: 352

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

Upvotes: 7

Related Questions