无法删除轮播上的文件

时间:2016-05-31 04:56:36

标签: c# carousel uwp

如何删除具有特定扩展名的文件?例如:.pdf

我试试这段代码:

StorageFolder library = await installedLocation.CreateFolderAsync("library", CreationCollisionOption.OpenIfExists);
BookAudio hapusmajalah = this.carousel.SelectedItem as BookAudio;
try
                        {
if(hapusmajalah.Name == hapusmajalah.Name + ".pdf")
{
    StorageFile filepdf = await library.GetFileAsync(hapusmajalah.Name + ".pdf");

                            await filepdf.DeleteAsync();
}

                            this.carousel.SelectedItem = carousel.Items[0];
                            await this.getContent();

                        }
                        catch
                        {
                            this.carousel.SelectedItem = carousel.Items[0];
                            this.getContent();
                        }
                    }));
}

BookAudio类:

class BookAudio
    {
        public string Name { get; set; }

        public ImageSource Image { get; set; }
    }

但未成功删除。如果不使用if,则表示文件已成功删除。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用path.getextension。

参考文献是:

using System.Linq;
using System.IO;

主要代码是:

#region //Remove files with the extensions in the list.
    public static void deleteFileExtensions(List<string> fileExtensionsToRemove, string filepath)
    {
        try
        {
            /*Get the extension by splitting by the final period.*/
            string tocompare = Path.GetExtension(filepath);
            /*For every element in the list, if one of the extensions matches the file extension, delete*/
            fileExtensionsToRemove.ForEach(x => { if (tocompare.Contains(x)) File.Delete(filepath); });
        }
        catch(IOException ex) { Console.WriteLine(ex); }
    }
    #endregion

然后你可以这样称呼它:

deleteFileExtensions(File.ReadAllLines("ExtensionsToDelete.txt").ToList(), "yourFilePath");