Can't delete file in application folder on Android/iOS with PCL-storage/interface

Oliv3rOliv3r USMember ✭✭
edited July 2017 in Xamarin.Forms

Hi!

I'm working on a Xamarin Forms project where I need to delete som files that have been stored in my application folder, but I won't get DeleteAsync to work.

GetFolderFromPathAsync, GetFileFromPathAsync and RenameAsync works just fine, but when I'm doing DeleteAsync, nothing happens. Can somebody please help me.

This is what my method looks like:

    public async Task DeleteFileAsync(string filepath)
    {
                var file = await FileSystem.Current.GetFileFromPathAsync(filepath);
                try
                {
                    await file.DeleteAsync();
                }
                catch (Exception ex)
                {
                    string str = ex.ToString();
                }
    }

Have also tried doing it like this:

public async Task DeleteFileAsync2(string filepath)
{
            string path = Path.GetDirectoryName(filepath);
            IFolder folder = await FileSystem.Current.GetFolderFromPathAsync(path);
            ExistenceCheckResult eex = ExistenceCheckResult.NotFound;
            String filename = Path.GetFileName(filepath);

            if (folder != null)
            {
                eex = await folder.CheckExistsAsync(filename);
            }

            if (eex == ExistenceCheckResult.FileExists)
            {
                var file = await folder.GetFileAsync(filename);
                try
                {
                    await file.DeleteAsync();           
                }
                catch (Exception ex)
                {
                    string str = ex.ToString();
                }
            }
}                     

And with interface and DependencyService using System.IO.File.Delete(source) on the native platform.

Have only tried it on Android so far, but are going to implement it on iOS in the future.

Best Answer

  • Oliv3rOliv3r US ✭✭
    Accepted Answer

    Solved it myself, can Admin delete post?

Answers

  • Oliv3rOliv3r USMember ✭✭
    Accepted Answer

    Solved it myself, can Admin delete post?

Sign In or Register to comment.