UWP StorageFolder.GetFilesAsync() returns System.__ComObject

641 views Asked by At

In UWP app I'm trying to access images from app storage folder using StorageFolder.GetFilesAsync() nethod.

    public async void GetMediaFiles()
    {

        _localMediaFolder = await GetOrCreateFolder(ApplicationData.Current.LocalFolder, "Media", ReservationId.ToString());
        IReadOnlyList<StorageFile> files = await _localMediaFolder.GetFilesAsync();

    }

Iv'e tried to use known folder as well with the same result.

    public async void GetMediaFiles()
    {

        StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
        _tempMediaFolder = await picturesFolder.CreateFolderAsync("media", CreationCollisionOption.OpenIfExists);
        IReadOnlyList<StorageFile> storageFilesOperation = await _tempMediaFolder.GetFilesAsync();

    }

Instead of 'IReadOnlyList' Im getting 'System.__ComObject'. I can't cast it to 'IReadOnlyList' and don't know how to use it to get the files i need.

Iv'e tried to use known folder as well with the same result. there is no exception int the Output indicating that the app have no access to the file or folder. Iv'e also checked the app manifest for access for the pictures library.

How Can I Fix this?

1

There are 1 answers

0
A. Milto On

This is by design. There's no need to cast System.__ComObject to IReadOnlyList because IReadOnlyList is an interface, which means when you create a variable of this type you can assign to it an instance of any class that implements the IReadOnlyList interface, and System.__ComObject is such class. You can continue working with storageFilesOperation as usual, e.g. delete all files:

StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
var _tempMediaFolder = await picturesFolder.CreateFolderAsync("media", CreationCollisionOption.OpenIfExists);
IReadOnlyList<StorageFile> storageFilesOperation = await _tempMediaFolder.GetFilesAsync();

foreach(StorageFile f in storageFilesOperation)
{
    await f.DeleteAsync();
}