Muhammad Touseef
Muhammad Touseef

Reputation: 4455

uwp NeighbourFilesQuery sometimes gives System.OverflowException

I use File​Activated​Event​Args args to do the file association in my video player app. So I when user double clicks a video file it opens and plays the file in my app, also it gets all the neighbouring files, so I can add them to playlist as well. I use following code for this purpose.

var file=args.Files[0]; 
StorageFileQueryResult neighboringFilesQuery=args.NeighboringFilesQuery;
var startingIndexunit = await neighboringFilesQuery.FindStartIndexAsync(file);
int startingIndex = Convert.ToInt32(startingIndexunit); //this is where exception occurs

not always but sometimes when I open a file I get a System.OverFlowException because the code tries to enter a very large garbage number into int32 which causes the exception.

After further investigation I have discovered that usually when I double click a file and get neighbor files, I get all the files in NeighborFilesQuery (including the 1 file I clicked to open) so I can just find its index so I can set the start index of my playlist in my app, and play the clicked file at correct position.

But in some other cases for example when I open a .flv or some a .rm file, I get all neighbor files in the neighborfilesQuery but I don't get the file I clicked, so when the code tries to get the index of that file, that file doesn't exist in that list hence I get a garbage index.

So why is this api so inconsistent? Sometimes it includes the clicked file in query files list, and sometimes it doesn't?

Note please note that I am only talking about a single file clicked scenario here and not about multiple files opened together (because in that case the files query is supposed to be empty and that is a different scenario).

Upvotes: 1

Views: 145

Answers (1)

David Hollowell - MSFT
David Hollowell - MSFT

Reputation: 1065

According to the documentation, when FindStartIndexAsync, you should expect to see uint.MaxValue. https://learn.microsoft.com/en-us/uwp/api/windows.storage.search.storagefilequeryresult.findstartindexasync

If we check the NeighboringFilesQuery documentation here: https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.activation.fileactivatedeventargs.neighboringfilesquery, we see that it's not available for all invocations. We'll also notice that none of the mp4 files show in the NeighboringFileQuery, so when we activate by clicking on the mp4, the mp4 is still not in the NeighboringFileQuery list. When you call to get the index, it returns uint.MaxValue since the requested file is not in the query. The exception occurs due to an overflow when trying to convert this to Int32.

For this case, you'll want to check against uint.MaxValue prior to casting/converting to Int32. You should also catch any exception that might occur, since you'll get the overflow for any value greater than int.MaxValue.

Upvotes: 0

Related Questions