我正在编写一个windows 8.1 (winrt)应用程序。我必须从path获得StorageFile (string)。我将进入子文件夹,直到迭代地使用此方法获得文件为止。(方法循环直到找到storageFile)
但是如何返回这个storageFile呢?它从第一次运行(循环)中返回空。
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。
发布于 2017-02-26 13:02:09
忘记分配file变量:
await GetStorageFileAsync(subFolder, newFilePath);应该是
file = await GetStorageFileAsync(subFolder, newFilePath);没有尝试代码,但这显然是结果是null的一个原因。
https://stackoverflow.com/questions/42468608
复制相似问题