mru.CheckAccess() confirms acces to not existing file

BitSmithy 1,751 Reputation points
2024-03-08T16:27:02.44+00:00

Hello,

I have an ListView where I show recently used files. Now wehen user clicks on any item i read selected file. I use such code.

private async void lvRecentlyUsedFiles_ItemClick(object sender, ItemClickEventArgs e)        
{            
	if (e.ClickedItem is IStorageItem isi && isi is StorageFile sf)            
	{                
	var mru = 		Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
                
	if(mru.CheckAccess(sf))                    
           ReadFile(sf);//this function reads file
            
	}        
}

The problem is that code mru.CheckAccess(sf) - confirms acces to files that don't exists. Isn't it a bug? How to check if file exists in my case?

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 14,831 Reputation points Microsoft Vendor
    2024-03-14T08:14:47.5333333+00:00

    Hi @BitSmithy ,

    Welcome to Microsoft Q&A!

    In the CheckAccess document.

    Determines whether the app has access to the specified storage item in the most recently used (MRU) list.

    CheckAccess only checks the permissions of the file in the MRU, not the file itself, so it cannot check if the file still exists.

    If you use the Remove method to remove the file in MRU list, then CheckAccess will return false.

    Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Remove(mruToken);
    
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 40,816 Reputation points
    2024-03-08T20:11:35.92+00:00

    Isn't it a bug?

    MRU = Most recently used, what does not mean, the file still exist; no bug, by design.