2016-05-31 7 views
0

特定の拡張子を持つファイルを削除するにはどうすればよいですか?たとえば:カルーセル上のファイルを削除できません

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

ではなく、正常に削除された.pdf

私はこのコードを試してみてください。 ifを使用しない場合、ファイルは正常に削除されています。この問題を解決するには?

答えて

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"); 
+0

は、どのような "yourFilePath" の意味ですか? 私のプロジェクトでは、 "library"上のファイルStorageFolder – Rose

関連する問題