No definition for GetFilesAsync

805 views Asked by At

Whenever i try this code:

  foreach (StorageFile filesAsync in await folderFromPathAsync.GetFolderAsync(_selectedPlayList).GetFilesAsync())//Error here
        {
            this._fileNames.Add(filesAsync.Name.Substring(0, filesAsync.Name.LastIndexOf('.')));
            this.FilesListBox.Items.Add(filesAsync.Name.Substring(0, filesAsync.Name.LastIndexOf('.')));
            this._pathNames.Add(filesAsync.Path);
        }

The definition GetFilesAsync() doesnt exist for some weird reason.Is this a fault of me? My full error message:

'Windows.Foundation.IAsyncOperation<Windows.Storage.StorageFolder>' does not contain a definition for 'GetFilesAsync' and no extension method 'GetFilesAsync' accepting a first argument of type 'Windows.Foundation.IAsyncOperation' could be found(are you missing a using directive or an assembly refrence?)

Please help me

1

There are 1 answers

2
Jon Skeet On

You're trying to call GetFilesAsync() on the result of calling GetFolderAsync() - whereas you should be awaiting the result of GetFolderAsync() and then calling GetFilesAsync() on the result of the await. The return value from GetFolderAsync() isn't the folder - it's the asynchronous operation which is fetching the folder. (It's an IAsyncOperation<StorageFolder>, not a StorageFolder.)

For example:

var folder = await folderFromPathAsync.GetFolderAsync(_selectedPlayList);
foreach (var file in await folder.GetFilesAsync())
{
    ...
}