首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中用自测圆法返回数据

在C#中用自测圆法返回数据
EN

Stack Overflow用户
提问于 2017-02-26 12:39:59
回答 1查看 68关注 0票数 1

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

但是如何返回这个storageFile呢?它从第一次运行(循环)中返回

代码语言:javascript
复制
 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;
            }
        }

它进入方法,检查路径字符串中是否存在子文件夹,并深入到该子文件夹中,最后进入条件的其他部分并获取文件。它不返回这个文件,但是它在循环的第一次执行中返回对象null。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-26 13:02:09

忘记分配file变量:

代码语言:javascript
复制
await GetStorageFileAsync(subFolder, newFilePath);

应该是

代码语言:javascript
复制
file = await GetStorageFileAsync(subFolder, newFilePath);

没有尝试代码,但这显然是结果是null的一个原因。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42468608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档