I am writing a windows phone 8.1 (winrt) app. I have to get StorageFile from path (string). I am entering into subfolders till I get file using this method iteratively. (Method loops till it finds storageFile)
But how to return this storageFile? It returns null from first run(loop).
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;
}
}
It enters into method, checks for presence of subfolder in path string and gets deeper into that subfolder, and at last enters into else part of condition and gets file. It doesnt return this file, but it returns object null from first execution of loop.
You forgot assigning the file
variable:
await GetStorageFileAsync(subFolder, newFilePath);
should be
file = await GetStorageFileAsync(subFolder, newFilePath);
Didn't try the code, but that would be an obvious reason of the result being null
.