How delete file in localstorage on winrt?
Asked Answered
C

3

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

            }


    }
Cordon answered 20/2, 2013 at 11:18 Comment(5)
Perhaps the file is opened some where else ? IF you can see it in a picture control or someting like that the delete couldn't runVientiane
I'll look in that directionCordon
If you loaded the photo into a BitmapDecoder or BitmapImage it would be locked as long as the BitmapDecoder or BitmapImage is used if you don't specify BitmapCacheOption.OnLoad. Then you get access denied as well.Victim
I still can't delete... any ideas ?Cordon
I am use bellow line to delete all of the data for my app. await ApplicationData.Current.ClearAsync();Quarrier
G
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 
        }
    }
Gonzalogoo answered 22/2, 2013 at 7:9 Comment(1)
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.Scutum
H
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

Hoggard answered 23/2, 2015 at 8:27 Comment(0)
S
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;
    }
Scutum answered 13/6, 2015 at 15:33 Comment(3)
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?Corbeil
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.Corbeil
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.Scutum

© 2022 - 2024 — McMap. All rights reserved.