C# UWP : How to check the downloads directory for presence of a file

Keith Crotty 81 Reputation points
2020-11-19T02:03:48.027+00:00

I have an "Import" button in my app.

If there is a file with a .xxxZip extension present, I want to enable that button and will use the File Picker to get it. Else, dont show the button.

Any way to do this?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yan Gu - MSFT 2,676 Reputation points
    2020-11-19T07:19:50.903+00:00

    Hello,

    Welcome to Microsoft Q&A.

    If you want to enable a button when there is a .xxxZip extension present in user’s Downloads folder, you need to track the Downloads folder’s changes by using StorageFileQueryResult.ContentsChanged Event which will be fired when a file is added to, deleted from, or modified in the folder being queried.

    The capability broadFileSystemAccess must be declared first in manifest like this and then you need to configure your app to get the permission of [access to the file system(https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions#capabilities-for-accessing-other-locations).]

    You could check the following code as a sample:

        public async void AddMonitor()  
        {  
            //Get the DownloadsFolder  
            StorageFile file =await DownloadsFolder.CreateFileAsync("test.txt", CreationCollisionOption.GenerateUniqueName);  
            StorageFolder folder =await file.GetParentAsync(); //The broadFileSystemAccess capability must be declared first, or the value of folder                                     will be null.  
            await file.DeleteAsync();  
            if(folder!=null)  
           {  
                //Add the monitor of the Downloads folder  
                StorageLibraryChangeTracker tracker = folder.TryGetChangeTracker();  
                if(tracker!=null)  
                {  
                    tracker.Enable();  
                    List<string> fileTypeFilter = new List<string>();  
                    fileTypeFilter.Add("*");  
                    var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);  
                    var queryResult = folder.CreateFileQueryWithOptions(queryOptions);  
                    await queryResult.GetFilesAsync();  
                    queryResult.ContentsChanged += QueryResult_ContentsChanged;  
                }  
            }  
        }  
    
        //When changes occur in user’s Downloads folder, QueryResult_ContentsChanged method will be fired.  
        private async void QueryResult_ContentsChanged(IStorageQueryResultBase sender, object args)  
        {  
            StorageFolder folder = sender.Folder;  
            if (folder != null)  
            {  
                StorageLibraryChangeReader changeReader =folder.TryGetChangeTracker().GetChangeReader();  
                IReadOnlyList<StorageLibraryChange> changeSet =await changeReader.ReadBatchAsync();  
    
                if(changeSet.Count>0)  
                {  
                    List<string> fileTypeFilter = new List<string>();  
                    fileTypeFilter.Add(".zip");  
                    var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);  
                    var query = folder.CreateFileQueryWithOptions(queryOptions);  
                    IReadOnlyList<StorageFile> fileList = await query.GetFilesAsync();  
                    if (fileList.Count > 0)  
                    {  
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
                        {  
                            button.Visibility = Visibility.Visible; //button is the Button which used to pick the zip file  
                        });  
                    }  
                    else  
                    {  
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
                        {  
                            button.Visibility = Visibility.Collapsed;  
                        });  
                    }  
                }  
                await changeReader.AcceptChangesAsync();  
            }  
        }  
    

    Note, you could get more information about the usage of StorageLibraryChangeReader class in the document.


    If the response is helpful, please click "Accept Answer" and upvote it.
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.