C# 从C中的自循环方法返回数据#

C# 从C中的自循环方法返回数据#,c#,windows-phone-8,windows-runtime,windows-phone-8.1,windows-phone,C#,Windows Phone 8,Windows Runtime,Windows Phone 8.1,Windows Phone,我正在编写一个windows phone 8.1(winrt)应用程序。我必须从路径(string)获取StorageFile。 我将进入子文件夹,直到使用此方法迭代地获取文件。(方法循环,直到找到storageFile) 但是如何返回这个存储文件呢? 它从第一次运行(循环)返回null 公共静态异步任务GetStorageFileAsync(StorageFolder currentFolder,string filePath) { StorageFile=null; if(FileHelpe

我正在编写一个windows phone 8.1(winrt)应用程序。我必须从路径(string)获取StorageFile。 我将进入子文件夹,直到使用此方法迭代地获取文件。(方法循环,直到找到storageFile)

但是如何返回这个存储文件呢? 它从第一次运行(循环)返回null

公共静态异步任务GetStorageFileAsync(StorageFolder currentFolder,string filePath) { StorageFile=null; if(FileHelper.IfPathContainDirectory(filePath)) { //只需获取文件夹。 string subFolderName=Path.GetDirectoryName(filePath); bool isSubFolderExist=wait FileHelper.iffolderexistasync(当前文件夹,子文件夹名称); StorageFolder子文件夹=null; 如果(isSubFolderExist) { //只需获取文件夹。 子文件夹= 等待currentFolder.GetFolderAsync(子文件夹名称); } 其他的 { 返回null; } 字符串newFilePath=Path.GetFileName(filePath); 如果(!string.IsNullOrEmpty(newFilePath)) { //以迭代方式获取文件。 等待GetStorageFileAsync(子文件夹,newFilePath); } 返回文件; } 其他的 { 尝试 { file=wait currentFolder.GetFileAsync(文件路径); } catch(异常fileExp) { } 返回文件; } } 它进入方法,检查路径字符串中是否存在子文件夹 并深入到该子文件夹,最后进入else部分 条件和获取文件。它不会返回此文件,但会返回
第一次执行循环时对象为空。

您忘记分配
文件
变量:

await GetStorageFileAsync(subFolder, newFilePath);
应该是

file = await GetStorageFileAsync(subFolder, newFilePath);

没有尝试该代码,但这将是导致结果为
null

的一个明显原因。您可以传递第三个参数,该参数是StorageFile类型(ref),并在找到后分配给它。异步方法不能有ref或out参数:(
file = await GetStorageFileAsync(subFolder, newFilePath);