2013-02-20 14 views
6

我嘗試無法成功刪除本地存儲中的文件。確實,我拍了一張照片,我想稍後用一個按鈕來刪除它,例如。但是,當我點擊按鈕,應用程序錯誤,我有:「訪問被拒絕」。如何在winrt上刪除localstorage中的文件?

我得到一個簡單的Delet.Async()後,我在StorageFile中的文件。

private async void delete_click(object sender, RoutedEventArgs e) 
    { 

      StorageFile filed = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg"); 
      if (filed != null) 
      { 
       await filed.DeleteAsync(); 

      } 


    } 
+3

也許文件被打開了一些地方?如果你可以看到它在一個圖片控件或類似的刪除無法運行 – 2013-02-20 11:31:54

+0

我會朝着那個方向 – Sw1a 2013-02-20 13:13:35

+3

如果您將照片加載到BitmapDecoder或BitmapImage它將被鎖定,只要BitmapDecoder或BitmapImage是如果您未指定BitmapCacheOption.OnLoad,則使用該屬性。那麼你也會拒絕訪問。 – 2013-02-20 15:10:28

回答

7

請嘗試下面的代碼,看看它是否適合你。

private async void takephoto_click(object sender, RoutedEventArgs e) 
    { 


     var ui = new CameraCaptureUI(); 
     ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3); 
     var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo); 

     if (file != null) 
     { 
      // store the file 
      var myFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("myImg.jpg"); 
      await file.MoveAndReplaceAsync(myFile); 

      // display the file 
      var bitmap = new BitmapImage(); 
      bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read)); 
      Photo.Source = bitmap; 
     } 



    } 

    private async void delete_click(object sender, RoutedEventArgs e) 
    { 
     StorageFile filed = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg"); 
     if (filed != null) 
     { 
      await filed.DeleteAsync(); 
     } 

     StorageFile filefound = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg"); 

     if (filefound != null) 
     { 
      // do something here 
     } 
    } 
+0

我會試試。謝謝 – Sw1a 2013-02-26 08:46:02

+0

此解決方案的問題在於,鑑於當前實現了Windows RT,第二個「GetFileAsync」操作不會返回'null'。相反,當'GetFileAsync'無法找到請求的文件時,它會創建並返回一個空文件,因此,使用此代碼,您永遠無法確認刪除有問題的文件。 一個優越的解決方案是在try/catch塊內部調用等待的'DeleteAsync'函數,然後處理異常 - 一個異常將表明所請求的刪除失敗,而沒有錯誤的和有問題的第二個'GetFileAsync'調用。 – Epsilon3 2015-06-13 15:29:45

0

我在從本地存儲中刪除文件時遇到同樣的問題。本地存儲中存在許多不同名稱的文件,因此如何刪除其他文件。在上述情況下,您已將硬編碼的文件名刪除。

StorageFile filefound = await ApplicationData.Current.LocalFolder.GetFileAsync(「myImg.jpg」); 而不是myImg.jpg用戶想刪除其他文件,那麼用戶將如何刪除

0
/// <summary> 
    /// Delete the indicated application file 
    /// </summary> 
    /// <param name="strFilePathName">The file path name to delete</param> 
    /// <returns>True, if successful; else false</returns> 
    public async static Task<bool> DeleteAppFile(string strFilePathName) 
    { 
     try 
     { 
      StorageFile fDelete = null; 

      if (!strFilePathName.Equals("")) 
      { 
       fDelete = await ApplicationData.Current.LocalFolder.GetFileAsync(strFilePathName); 
       if (fDelete != null) 
       { 
        try 
        { 
         await fDelete.DeleteAsync(); 
        } 
        catch (Exception ex) 
        { 
         AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile {" + strFilePathName + "}", ex.Message); 

         return false; 
        } 

        return true; 
       } 
      } 
      else 
       AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile", "File path name is empty."); 
     } 
     catch (Exception ex) 
     { 
      AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile {" + strFilePathName + "}", ex.Message); 
     } 

     return false; 
    } 
+0

在什麼情況下'DeleteAsync'會引發異常?智能感知不表示可能拋出任何異常。此外,如果文件存在,是否保證成功? – 2017-03-26 02:50:58

+0

如果文件不存在,它實際上會拋出'FileNotFoundException'。我不認爲有必要在'try/catch'中放入'DeleteAsync'調用,因爲如果可以創建一個'StorageFile'對象,它就存在。如果文件不存在,MSDN上沒有任何內容會提示返回空的「StorageFile」對象;因此,'if(fDelete!= null)'就足夠了。我不認爲訪問是一個問題,或者是因爲您被自動授予對本地存儲的訪問權限。 – 2017-03-26 02:58:31

+0

try/catch語法是習慣 - 塊中還有其他可能會拋出異常的語句 - 而不是將我的代碼定製爲不存在的罕見位,我把它們全部封裝起來。我使用太多的bug程序,不花時間去嘗試()任何東西。 – Epsilon3 2017-04-07 16:15:00

相關問題