2014-11-22 55 views
0

我需要獲取文件夾(廣告子文件夾)內所有文件的文件詳細信息。我已經爲此寫了一個遞歸函數。在遞歸函數中沒有得到更新的值Windows Phone 8

private async Task getallfiles(StorageFolder appfolder) 
    { 
     IReadOnlyList<StorageFile> sortedItems1 = await appfolder.GetFilesAsync(); 
     if (sortedItems1.Count > 0) 
     { 
      foreach (StorageFile file in sortedItems1) 
       CopyContentToIsolatedStorage(file.Path); 
     } 

     IReadOnlyList<StorageFolder> sorteditems2 = await appfolder.GetFoldersAsync(); 
     if (sorteditems2.Count > 0) 
     { 
      foreach (StorageFolder folder in sorteditems2) 
       await getallfiles(folder); 
     } 
    } 

現在,當我打電話與作爲參數傳遞的根文件夾這個功能,我正在裏面sorteditems根文件夾,這是一個全局變量只有文件。我嘗試傳遞不同的文件夾作爲參數,但每次返回的已排序項只包含父文件夾中的文件傳遞,而不包含子文件夾中的文件。

我是否錯過了某些東西,或者是否存在某些代碼的邏輯錯誤。任何幫助,將不勝感激。

例外我得到 - 它可能找到問題的幫助:

{System.NullReferenceException:對象不設置到對象的實例。 在Appzillon.MainPage.d__3d.MoveNext() ---從先前的位置在那裏引發異常--- 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務task) 堆棧跟蹤System.Runtime結束。 CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Appzillon.MainPage.d__36.MoveNext() ---從之前位置拋出異常的堆棧跟蹤結束 - - at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__0(Object state)}。

而且功能CopyContentToIsolatedStorage如下:

public static void CopyContentToIsolatedStorage(string file) 
    { 
     // Obtain the virtual store for the application. 
     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 

     if (iso.FileExists(file)) 
      return; 

     var fullDirectory = System.IO.Path.GetDirectoryName(file); 
     if (!iso.DirectoryExists(fullDirectory)) 
      iso.CreateDirectory(fullDirectory); 

     // Create a stream for the file in the installation folder. 
     using (Stream input = Application.GetResourceStream(new Uri(file, UriKind.Relative)).Stream) 
     { 
      // Create a stream for the new file in isolated storage. 
      using (IsolatedStorageFileStream output = iso.CreateFile(file)) 
      { 
       // Initialize the buffer. 
       byte[] readBuffer = new byte[4096]; 
       int bytesRead = -1; 

       // Copy the file from the installation folder to isolated storage. 
       while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0) 
       { 
        output.Write(readBuffer, 0, bytesRead); 
       } 
      } 
     } 
    } 

編輯:更新的代碼,增加了我得到的例外。我只得到根文件夾文件的原因是因爲我已經將sortedItems作爲Readonly,正如kennyzx所提到的。但即使嘗試了他的修改後的代碼,並且實際上也將其更改爲上述更新的代碼,但我仍然遇到問題。請參閱我的意見kennyzx的答案...

問候

回答

0

你可能已經宣佈sortedItemsIReadOnlyList<StorageFile>,所以sortedItems不能被修改(這就是爲什麼它被稱爲ReadOnlyList);在您的代碼中,Concat方法生成一個新列表結合sortedItemssortedItems1,這些文件實際上並未添加到sortedItems

所以首先聲明sortedItemsList<StorageFile>,它支持將另一個列表添加到本身

在遞歸調用方法之前,您忘記了await關鍵字。

List<StorageFile> sortedItems = new List<StorageFile>(); 

private async Task getallfiles(StorageFolder appfolder) 
{ 
    IReadOnlyList<StorageFile> sortedItems1 = await appfolder.GetFilesAsync();   
     sortedItems.AddRange(sortedItems1); 

    IReadOnlyList<StorageFolder> sorteditems2 = await appfolder.GetFoldersAsync(); 
    if (sorteditems2.Count > 0) 
    { 
     foreach (StorageFolder folder in sorteditems2) 
      await getallfiles(folder); //add await 
    } 
} 
+0

感謝您的回答。就在我看到你的答案之前,我在某處閱讀了有關異步任務的內容,並將我的功能更改爲異步任務,並添加了等待。但是,當你猜對的時候,我對已分類項目的聲明是錯誤的。但是,儘管發生了這些變化,代碼仍然處於兩者之間。由於我的實際動機是將文件複製到獨立存儲,我爲此寫了一個函數CopyContentToIsolatedStorage(StorageFolder文件夾)。我決定用sortedItems1列表中的foreach函數替換AddRange(以前的concat)語句和上面的函數。 cond ... – 2014-11-24 12:42:35

+0

新替換的代碼如下所示:'if(sortedItems1.Count> 0) { foreach(StorageFile file in sortedItems1) CopyContentToIsolatedStorage(file.Path); }而不是'sortedItems.AddRange(sortedItems1);'即使在這之後,代碼也被打破了同樣的錯誤。但我似乎已經理解的一件事是,它是一個異步問題,只是在應用程序崩潰之後,當我檢查應用程序的隔離存儲,複製的文件以及... contd ... – 2014-11-24 12:42:58

+0

您可以編輯自己的問題使用新的代碼,併發布'CopyContentToIsolatedStorage'的實現。 – kennyzx 2014-11-24 12:47:44