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.