2010-10-08 48 views
3

我目前正在研究將數據寫入IsolatedStorageStore的應用程序。作爲應用程序的一部分,我想實現一個「清除所有數據/重置」按鈕,但是通過所有存在的文件和存在的所有文件夾進行枚舉需要相當多的時間。是否有一種神奇的「重置」方法或我可以使用的方法,還是我應該專注於優化手動刪除過程?清除windows phone 7應用程序的隔離存儲庫最快的方法是什麼?

或者我可以逃避不提供這樣的功能,並讓用戶卸載/重新安裝應用程序重置?

我猙獰刪除,所有文件的方法如下:

/// <summary> 
    /// deletes all files in specified folder 
    /// </summary> 
    /// <param name="sPath"></param> 
    public static void ClearFolder(String sPath, IsolatedStorageFile appStorage) 
    {  
     //delete all files 
     string[] filenames = GetFilenames(sPath); 
     if (filenames != null) 
     { 
      foreach (string sFile in filenames) 
      { 
       DeleteFile(System.IO.Path.Combine(sPath, sFile)); 
      } 
     } 

     //delete all subfolders if directory still exists 
     try 
     { 
      foreach (string sDirectory in appStorage.GetDirectoryNames(sPath)) 
      { 
       ClearFolder(System.IO.Path.Combine(sPath, sDirectory) + @"\", appStorage); 
      } 
     } 
     catch (DirectoryNotFoundException ex) 
     { 
      //current clearing folder was deleted/no longer exists - return 
      return; 
     } 

     //try to delete this folder 
     try 
     { 
      appStorage.DeleteDirectory(sPath); 
     } 
     catch (ArgumentException ex) { } 

    } 

    /// <summary> 
    /// Attempts to delete a file from isolated storage - if the directory will be empty, it is also removed. 
    /// </summary> 
    /// <param name="sPath"></param> 
    /// <returns></returns> 
    public static void DeleteFile(string sPath) 
    { 
     using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      appStorage.DeleteFile(sPath); 

      String sDirectory = System.IO.Path.GetDirectoryName(sPath); 
      //if this was the last file inside this folder, remove the containing folder 
      if (appStorage.GetFileNames(sPath).Length == 0) 
      { 
       appStorage.DeleteDirectory(sDirectory); 
      } 
     } 
    } 

    /// <summary> 
    /// Returns an array of filenames in a given directory 
    /// </summary> 
    /// <param name="sHistoryFolder"></param> 
    /// <returns></returns> 
    public static string[] GetFilenames(string sDirectory) 
    { 
     using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      try 
      { 
       return appStorage.GetFileNames(sDirectory); 
      } 
      catch (DirectoryNotFoundException) 
      { 
       return null; 
      } 
     } 
    } 

回答

6

您正在尋找的Remove()方法。

使用方法如下:

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    store.Remove(); 
} 
+0

感謝堆!我想我曾嘗試過使用這種方法,但之前我一直在做錯誤的事情,因爲它曾經拋出異常......無論如何。謝謝 :) – 2010-10-08 23:18:44

相關問題