UWP入门(十)--获取文件属性

简介: 原文:UWP入门(十)--获取文件属性 重要的 API StorageFile.GetBasicPropertiesAsync StorageFile.Properties StorageItemContentProperties.RetrievePropertiesAsync 1. 获取文件的顶级属性 很多顶级文件属性都可以作为 StorageFile 类的成员进行访问。
原文: UWP入门(十)--获取文件属性

重要的 API

  • StorageFile.GetBasicPropertiesAsync
    • StorageFile.Properties
    • StorageItemContentProperties.RetrievePropertiesAsync

1. 获取文件的顶级属性

很多顶级文件属性都可以作为 StorageFile 类的成员进行访问。 这些属性包括文件属性、内容类型、创建日期、显示名称和文件类型等

注意: 请记住,要声明 picturesLibrary 功能

// Enumerate all files in the Pictures library.
var folder = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();

foreach (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);
}

2.获取文件的基本属性

很多基本文件属性都通过先调用 StorageFile.GetBasicPropertiesAsync 方法获得。 此方法会返回一个 BasicProperties 对象,该对象将定义项(文件或文件夹)的大小属性,以及上次修改项的时间。

此示例枚举了图片库中的所有文件,从而访问每个文件中的一些基础属性

// 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();

    // Get file's basic properties.
    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);
}

3. 获取文件的扩展属性(用时候再看)

除了顶级和基本文件属性之外,还有一些与文件内容有关的属性。 这些扩展属性可以通过调用 BasicProperties.RetrievePropertiesAsync 方法来访问。 (通过调用 StorageFile.Properties 属性可以获得 BasicProperties 对象。)尽管顶级和基本文件属性可以分别作为类的 StorageFile 和 BasicProperties 属性进行访问,但扩展属性只能通过以下方法获得:

将代表将要检索的属性名称的 String 对象的 IEnumerable 集合传递到 BasicProperties.RetrievePropertiesAsync 方法。 此方法随后会返回一个 IDictionary 集合。 然后,可以按名称或按索引从该集合中检索每个扩展属性。

以下示例枚举了图片库中的所有文件,并指定了一个 List 对象中所需属性(DataAccessed 和 FileOwner)的名称,将该 List 对象传递到 BasicProperties.RetrievePropertiesAsync 以检索这些属性,然后按名称从返回的 IDictionary 对象中检索这些属性

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 (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);
    }
}
目录
相关文章
|
6月前
SAP UI5 控件 ObjectStatus 的使用方法介绍试读版
SAP UI5 控件 ObjectStatus 的使用方法介绍试读版
50 0
SAP UI5 控件 ObjectStatus 的使用方法介绍试读版
|
XML 物联网 开发工具
(1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
原文:(1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
1229 0
|
开发工具 git
(2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
原文:(2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
913 0
|
数据库 C# 索引
通通WPF随笔(1)——基于lucene.NET让ComboBox拥有强大的下拉联想功能
原文:通通WPF随笔(1)——基于lucene.NET让ComboBox拥有强大的下拉联想功能       我一直很疑惑百度、谷哥搜索框的下拉联想功能是怎么实现的?是不断地查询数据库吗?其实到现在我也不知道,他们是怎么实现这么高效的。
1343 0
|
.NET C# C++
通通WPF随笔(2)——自己制作轻量级asp.net网站服务
原文:通通WPF随笔(2)——自己制作轻量级asp.net网站服务          大学玩asp.net时就发现VS在Debug时会起一个web服务,这东西也太神奇了服务起得这么快,而相对于IIS又这么渺小。
1265 0
|
容器
使用Xamarin.Forms的企业应用程序模式(电子书)--前言
本电子书提供了使用Xamarin.Forms构建跨平台企业应用程序的指导。Xamarin.Forms是一个跨平台的UI工具包,允许开发人员轻松创建可以跨平台共享的本机用户界面布局,包括iOS,Android和通用Windows平台(UWP)。
1016 0