Return 来自 C# 中自循环方法的数据
Return data from self loopoing method in C#
我正在编写一个 windows phone 8.1 (winrt) 应用程序。我必须从路径 (string) 中获取 StorageFile。
我正在进入子文件夹,直到我迭代地使用此方法获取文件。 (方法循环直到找到 storageFile)
但是如何return这个storageFile呢?
它 returns 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 部分
条件并获取文件。它没有 return 这个文件,但是 returns
第一次执行循环时对象为 null。
您忘记分配 file
变量:
await GetStorageFileAsync(subFolder, newFilePath);
应该是
file = await GetStorageFileAsync(subFolder, newFilePath);
没有尝试代码,但这显然是结果 null
.
的原因
我正在编写一个 windows phone 8.1 (winrt) 应用程序。我必须从路径 (string) 中获取 StorageFile。 我正在进入子文件夹,直到我迭代地使用此方法获取文件。 (方法循环直到找到 storageFile)
但是如何return这个storageFile呢? 它 returns 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 部分 条件并获取文件。它没有 return 这个文件,但是 returns 第一次执行循环时对象为 null。
您忘记分配 file
变量:
await GetStorageFileAsync(subFolder, newFilePath);
应该是
file = await GetStorageFileAsync(subFolder, newFilePath);
没有尝试代码,但这显然是结果 null
.