1

我正在寫一個windows phone 8.1(winrt)應用程序。我必須從路徑獲得StorageFile字符串)。 我正在進入子文件夾,直到迭代地使用此方法獲取文件。 (方法循環直到找到storageFile)從C#中的自我循環方法返回數據#

但是如何返回這個storageFile? 它從第一次運行(循環)返回null

public static async Task<StorageFile> GetStorageFileAsync(StorageFolder currentFolder, string filePath) 
     { 
      StorageFile file = null; 
      if (FileHelper.IfPathContainDirectory(filePath)) 
      { 
       // Just get the folder. 
       string subFolderName = Path.GetDirectoryName(filePath); 

       bool isSubFolderExist = await FileHelper.IfFolderExistsAsync(currentFolder, subFolderName); 

       StorageFolder subFolder=null; 

       if (isSubFolderExist) 
       { 
        // Just get the folder. 
        subFolder = 
         await currentFolder.GetFolderAsync(subFolderName); 
       } 
       else 
       { 
        return null; 
       } 

       string newFilePath = Path.GetFileName(filePath); 

       if (!string.IsNullOrEmpty(newFilePath)) 
       { 
        //get file iteratively. 
        await GetStorageFileAsync(subFolder, newFilePath); 
       } 
       return file; 
      } 
      else 
      { 

       try 
       { 
        file = await currentFolder.GetFileAsync(filePath); 
       } 
       catch(Exception fileExp) 
       { 

       } 

       return file; 
      } 
     } 

它進入的方法,在路徑字符串 子文件夾中的存在檢查和變深成子文件夾,最後進入的狀態else部分 並獲取文件。它沒有返回這個文件,但是它從第一次執行循環返回 對象null。

+0

您可以傳遞一個StorageFile類型(ref)的第三個參數,並在找到該參數後將它分配給它。 – master2080

+1

異步方法不能有參考或輸出參數:( –

+0

從來不知道。 – master2080

回答

2

你忘分配file變量:

await GetStorageFileAsync(subFolder, newFilePath); 

應該

file = await GetStorageFileAsync(subFolder, newFilePath); 

沒試過的代碼,但是這將是其結果是null的一個明顯的原因。

+1

ops!是的,多麼愚蠢的錯誤。非常感謝 –