6

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

在 StorageFile 中获取文件后,我使用了一个简单的 Delete.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();

            }


    }
4

3 回答 3

7

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

    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 
        }
    }
于 2013-02-22T07:09:58.730 回答
0

我在从本地存储中删除文件时遇到了同样的问题。本地存储中存储了许多不同名称的文件,那么如何删除其他文件。在上述情况下,您已硬编码要删除的文件名。

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

于 2015-02-23T08:27:24.283 回答
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;
    }
于 2015-06-13T15:33:15.283 回答