Я использую функцию Windows.Storage GetFilesAsync для получения информации о файле, но обнаруживаю, что количество файлов часто неверно (по сравнению со свойствами ОС).Странно, но иногда это число меньше, а иногда больше, чем количество ОС!?
Я создал мини-проект, чтобы повторить проблему.На папках с очень маленьким количеством файлов они совпадают, но с большим количеством (то есть 500+) счет часто не совпадает.
Для репликации создайте пустое приложение Universal Windows, затем скопируйте его в 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>
Наконец, скопируйте его в MainPage.xaml.cs и запуститеприложение:
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;
}
}
}