简体   繁体   中英

Windows.Storage function returning wrong file count

I am using Windows.Storage GetFilesAsync function to retrieve file information, but am finding that the file count is often incorrect (compared to OS properties). Strangely sometimes the number is smaller and other times larger than the OS count!?

I've created a mini project to replicate the issue. On folders with very small file count, they do match, but with larger counts (ie 500+) the count is often way off.

To replicate create a Universal Windows blank app, then copy this to MainPage.xaml:

<Page
x:Class="TestFileCount.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestFileCount"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <Button Name="btnSelect" Content="Select Folder" HorizontalAlignment="Left" Height="195" Margin="254,412,0,0" VerticalAlignment="Top" Width="805" Click="btnSelect_Click"/>
    <TextBlock Name="txtFolder" HorizontalAlignment="Left" Height="92" Margin="185,212,0,0" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Width="1156"/>
    <TextBlock Name="txtResult"  HorizontalAlignment="Left" Height="163" Margin="96,701,0,0" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Width="1210"/>

</Grid>

Finally copy this to MainPage.xaml.cs and run the app:

using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Search;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


namespace TestFileCount
{
    public sealed partial class MainPage : Page
    {
      public MainPage()
      {
        this.InitializeComponent();
    }

    private async void btnSelect_Click(object sender, RoutedEventArgs e)
    {

        const string SizeProperty = "System.Size";
        const string DateModProperty = "System.DateModified";

        var folderPicker = new Windows.Storage.Pickers.FolderPicker
        {
            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop
        };

        folderPicker.FileTypeFilter.Add("*");


        StorageFolder folder = await folderPicker.PickSingleFolderAsync();

        //cancelled
        if (folder == null)
        {
            return;
        }

        txtResult.Text = "Processing...";
        txtFolder.Text = folder.Path;
        btnSelect.IsEnabled = false;

        // Set up file settings
        List<string> fileTypeFilter = new List<string>();
        List<string> propertyNames = new List<string>
        {
            SizeProperty,
            DateModProperty
        };

        // Create query options
        QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter)
        {
            FolderDepth = FolderDepth.Deep,
            IndexerOption = IndexerOption.UseIndexerWhenAvailable
        };
        queryOptions.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, propertyNames);

        StorageFileQueryResult query = folder.CreateFileQueryWithOptions(queryOptions);

        //get files
        IReadOnlyList<StorageFile> fileList = await query.GetFilesAsync();

        txtResult.Text = fileList.Count.ToString();
        btnSelect.IsEnabled = true;
    }
  }
}

It seem to be a known issue and it does not work because of OrderByName. Delete OrderByName.

QueryOptions queryOptions = new QueryOptions(){...};

It's mentioned here that it does not work outside of the library folder contrary to the documentation which states that it works everywhere. Even though I tested it on my PC and it worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM