如何在winrt上删除本地存储中的文件

本文关键字:存储 文件 删除 winrt | 更新日期: 2023-09-27 18:00:11

我尝试删除本地存储中的文件,但没有成功。没错,我拍了一张照片,我想稍后用一个按钮删除它。但当我点击按钮时,应用程序会出现错误,我会说:"访问被拒绝"。

在StorageFile中获取文件后,我生成了一个简单的Delet.Async()。

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

    }

如何在winrt上删除本地存储中的文件

尝试下面的代码,看看它是否适合您。

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

我在从本地存储中删除文件时遇到了同样的问题。本地存储器中存储了许多不同名称的文件,因此如何删除其他文件。在上面的情况下,您已经对要删除的文件名进行了硬编码。

StorageFile filefound=等待ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg");而不是myImg.jpg用户想要delte其他文件,然后用户将如何删除

D_3