我正在使用Windows.Storage GetFilesAsync函数检索文件信息,但是发现文件计数通常不正确(与OS属性相比)。奇怪的是,有时这个数字小于操作系统的数量,而有时却大于操作系统的数量!?
我创建了一个迷你项目来复制问题。在文件数量很少的文件夹中,它们确实可以匹配,但在文件数量较大(即500以上)的文件夹中,计数通常会很远。
要进行复制,请创建一个通用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;
}
}
}
答案 0 :(得分:1)
这似乎是一个已知问题,由于OrderByName,它不起作用。删除OrderByName。
QueryOptions queryOptions = new QueryOptions(){...};
提到here时,它与库文件夹之外的其他文档均相反,该文档指出它在任何地方都可以使用。 即使我在PC上对其进行了测试,但效果很好。