如何使用 xamarin 表单删除文件 [英] How to delete files using xamarin forms

查看:55
本文介绍了如何使用 xamarin 表单删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从特定目录(文件夹)中删除文件,但它对我不起作用

I tried to delete file from specific directory(Folder) but it doesn't work for me

这是我的代码

DeletePhoto.Clicked += async (sender, args) =>
            {
                var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
                {
                    PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,

                });


                if ( file != null)
                {
                    if (File.Exists(file.Path))
                    {
                        DependencyService.Get<IFileManager>().DeleteFile(file.Path);
                    }
                    file.Dispose();

                }
            };

在安卓中

  public class FileManager : IFileManager
        {
            public void DeleteFile(string source)
            {
                File.Delete(source);
            }
        }

IFileManager 接口

IFileManager Interface

 public interface IFileManager
    {
        void DeleteFile(string source);
    }

在 mainfest 中给出了许可,但没有发生什么,我找到了我删除的文件

In mainfest permisson is given but something didn't happen and i find the file which i deleted

推荐答案

问题是由PickPhotoAsync()方法引起的,这个方法是在blow目录下保存了一张图片的COPY:>

The problem is caused by PickPhotoAsync()method, this method is saving a COPY of the image in the blow directory:

"/storage/emulated/0/Android/data/{package name}/files/Pictures/temp/***.jpg"

你可以调试找到这个,你可以通过this

you can debug to find this and you can check this issue of MediaPlugin by this

所以当您使用PickPhotoAsync()方法选择文件时,您删除的实际文件是temp目录中的副本.

So when you use PickPhotoAsync()method to select a file, the actual file you have deleted is the copy in the temp directory.

如果要删除原文件,就得修改

If you want to delete the original file, you will have to change the parameter of

DeleteFile(string source)方法到原路径如下:

string path= "/storage/emulated/0/Android/data/{package name}/files/Pictures/Test/***.jpg";

DeleteFile(path);

这篇关于如何使用 xamarin 表单删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆