Search code examples
c#xamluwpcomexceptionstoragefile

uwp mostrecentlyusedlist addition causes COM type exception


StackTrace

" at Windows.Storage.AccessCache.StorageItemMostRecentlyUsedList.Add(IStorageItem file, String metadata, RecentStorageItemVisibility visibility)\r\n at FluentVideoPlayer.Helpers.FileHelper.<>c__DisplayClass7_0.b__0()\r\n at Microsoft.Toolkit.Uwp.Helpers.DispatcherHelper.<>c__DisplayClass10_0`1.b__0()"

I am trying to add a StorageFile to MostRecentlyUsedList and as a result I am getting this exception.

Exception

HRESULT E_FAIL has been returned from a call to a COM component

Code

internal async static Task AddToHistory(StorageFile fileToBeAdded) => await DispatcherHelper.ExecuteOnUIThreadAsync(() => StorageApplicationPermissions.MostRecentlyUsedList.Add(fileToBeAdded, "", RecentStorageItemVisibility.AppAndSystem));

I have this static method to use within a static class so I can call it from any page in the app. I can verify that StorageFile object is not null and perfect I also tried to solve it by using DispatcherHelper as you can see in the code, but with or without it, exception occurs in both cases.

Update

I have tried to add to FutureAccessList as well instead of MostRecentlyUsedList and I am getting same error in both cases

Update 2

accessing the list normally doesn't cause any error, like I can access it with following code

var mlist = StorageApplicationPermissions.MostRecentlyUsedList;
var entries = mlist.Entries;

error only occurs when I try to add a storagefile to it.


Solution

  • The problem was in partial StorageFiles I was actually querying the files from KnownFolders.VideoLibrary as per following blog post.

    https://blogs.msdn.microsoft.com/adamdwilson/2017/12/20/fast-file-enumeration-with-partially-initialized-storagefiles/

    so when we use following indexing option it actually initializes partial storage files for us which causes exception when we try to add it to most recently used list or futureacceslist

    IndexerOption = IndexerOption.OnlyUseIndexerAndOptimizeForIndexedProperties
    

    So to solve this I am now using IndexerOption.UseIndexerWhenAvailable now I don't get any exception, but ofcourse I am sacrificing the speed I was getting before with partial storage files. Which is disappointing as when trying to do a job with full storagefile, it should automatically initialize the partial storagefile to full storagefile as per the blog post. but that is not the case here unfortunately.