重要的应用程序接口(API)
- StorageFile.GetBasicPropertiesAsync
- StorageFile.Properties
- StorageItemContentProperties.RetrievePropertiesAsync
获取 由 StorageFile 对象表示的文件的属性(顶级、基本和扩展)。
注释
有关完整示例,请参阅 文件访问示例。
先决条件
了解通用 Windows 平台(UWP)应用的异步编程
了解如何在 C# 或 Visual Basic 中编写异步应用,请参阅 在 C# 或 Visual Basic 中调用异步 API。 若要了解如何在C++中编写异步应用,请参阅C++中的
异步编程。 对位置 的访问权限
例如,这些示例中的代码需要 picturesLibrary 功能,但你的位置可能要求不同的功能或根本没有功能。 若要了解详细信息,请参阅 文件访问权限。
获取文件的顶级属性
许多顶级文件属性作为 StorageFile 类的成员可访问。 这些属性包括文件属性、内容类型、创建日期、显示名称、文件类型等。
注释
请记住声明 picturesLibrary 权限。
此示例枚举图片库中的所有文件,访问每个文件的一些顶级属性。
// Enumerate all files in the Pictures library.
var folder = Windows.Storage.KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();
foreach (Windows.Storage.StorageFile file in files)
{
StringBuilder fileProperties = new StringBuilder();
// Get top-level file properties.
fileProperties.AppendLine("File name: " + file.Name);
fileProperties.AppendLine("File type: " + file.FileType);
}
获取文件的基本属性
首先通过调用 StorageFile.GetBasicPropertiesAsync 方法获取许多基本文件属性。 此方法返回 BasicProperties 对象,该对象定义项目(文件或文件夹)的大小以及上次修改项目时的属性。
此示例枚举图片库中的所有文件,访问每个文件的基本属性中的一些。
// Enumerate all files in the Pictures library.
var folder = Windows.Storage.KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();
foreach (Windows.Storage.StorageFile file in files)
{
StringBuilder fileProperties = new StringBuilder();
// Get file's basic properties.
Windows.Storage.FileProperties.BasicProperties basicProperties =
await file.GetBasicPropertiesAsync();
string fileSize = string.Format("{0:n0}", basicProperties.Size);
fileProperties.AppendLine("File size: " + fileSize + " bytes");
fileProperties.AppendLine("Date modified: " + basicProperties.DateModified);
}
获取文件的扩展属性
除了顶级文件和基本文件属性之外,有许多与文件内容关联的属性。 通过调用 BasicProperties.RetrievePropertiesAsync 方法来访问这些扩展属性。 (BasicProperties 对象是通过调用 StorageFile.Properties 属性获取的。虽然顶级文件和基本文件属性可作为类的属性访问(StorageFile 和 BasicProperties),但扩展属性需要通过将表示属性名称的 String 对象的 IEnumerable 集合传递给 BasicProperties.RetrievePropertiesAsync 方法来获取。 然后,此方法返回 IDictionary 集合。 然后,按名称或索引从集合中检索每个扩展属性。
此示例枚举图片库中的所有文件,在 List 对象中指定所需属性的名称(DataAccessed 和 FileOwner),将该 List 对象传递给 BasicProperties.RetrievePropertiesAsync 以检索这些属性,然后按返回的 IDictionary 对象的名称检索这些属性。
如需文件扩展属性的完整列表,请参阅 Windows 核心属性。
const string dateAccessedProperty = "System.DateAccessed";
const string fileOwnerProperty = "System.FileOwner";
// Enumerate all files in the Pictures library.
var folder = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();
foreach (Windows.Storage.StorageFile file in files)
{
StringBuilder fileProperties = new StringBuilder();
// Define property names to be retrieved.
var propertyNames = new List<string>();
propertyNames.Add(dateAccessedProperty);
propertyNames.Add(fileOwnerProperty);
// Get extended properties.
IDictionary<string, object> extraProperties =
await file.Properties.RetrievePropertiesAsync(propertyNames);
// Get date-accessed property.
var propValue = extraProperties[dateAccessedProperty];
if (propValue != null)
{
fileProperties.AppendLine("Date accessed: " + propValue);
}
// Get file-owner property.
propValue = extraProperties[fileOwnerProperty];
if (propValue != null)
{
fileProperties.AppendLine("File owner: " + propValue);
}
}