原文: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);
}
}

UWP入门(十)--获取文件属性的更多相关文章

  1. UWP入门(十)--创建、写入和读取文件

    原文:UWP入门(十)--创建.写入和读取文件 核心的 API github代码 StorageFolder 类 StorageFile 类 FileIO 类 使用 StorageFile 对象读取和 ...

  2. UWP入门(十二)--数据绑定用法

    原文:UWP入门(十二)--数据绑定用法 主要几个元素: Template DataTemplate ItemSource 数据绑定是一个数据提取的方法,能使数据和UI上的控件紧密相连,下面的Demo ...

  3. win10 uwp 入门

    UWP是什么我在这里就不说,本文主要是介绍如何入门UWP,也是合并我写的博客. 关于UWP介绍可以参见:http://lib.csdn.net/article/csharp/32451 首先需要申请一 ...

  4. UWP入门(九)-- 枚举和查询文件和文件夹

    原文:UWP入门(九)-- 枚举和查询文件和文件夹 核心 API 所在的命名空间: Windows.Storage Windows.Storage.Streams Windows.Storage.Pi ...

  5. UWP入门(六)-- ResourceDictionary 和 XAML 资源引用

    原文:UWP入门(六)-- ResourceDictionary 和 XAML 资源引用 你最希望声明为 XAML 资源的 XAML 元素包括 Style.ControlTemplate.动画组件和 ...

  6. UWP 入门教程2——如何实现自适应用户界面

    系列文章 UWP入门教程1——UWP的前世今生 如上文所说的,布局面板根据可用的屏幕空间,指定界面元素的大小和位置.例如StackPanel 会水平或垂直排列界面元素.Grid 布局与CSS 中的表格 ...

  7. Android入门(十二)SQLite事务、升级数据库

    原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...

  8. 获取文件属性信息之stat、fstat和lstat

    UNIX文件系统是目录和文件组成的一种层次结构.目录(directory)是一个包含许多目录项的文件,在逻辑上,可以认为每个目录项都包含一个文件名,同时还包含说明该文件属性的信息.文件属性是指文件类型 ...

  9. OpenCV入门之获取验证码的单个字符(二)

      在文章 OpenCV入门之获取验证码的单个字符(字符切割)中,介绍了一类验证码的处理方法,该验证码如下: 该验证码的特点是字母之间的间隔较大,很容易就能提取出其中的单个字符.接下来,笔者将会介绍如 ...

随机推荐

  1. 【t008】钱币变换问题

    Time Limit: 2 second Memory Limit: 32 MB [问题描述] 给定 2*n 个方格,将其排成一行.选择两个相邻的方格,设置为空方格,初始时不放钱币.而其余的方格共放入 ...

  2. Word Break II -- leetcode

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  3. 【t017】YL杯超级篮球赛

    Time Limit: 1 second Memory Limit: 256 MB [问题描述] 一年一度的高一YL杯超级篮球赛开赛了.当然,所谓超级的意思是参赛人数可能多于5人.小三对这场篮球赛非常 ...

  4. zookeeper 客户端操作

    代码 /** * 创建zk客户端 * 实现循环监听的两个必要条件:1.程序不能结束2.递归调用监听器 * @author tele * */ public class Demo { ; //多个节点用 ...

  5. AsyncTask下载图片

    最近在看一个非常早期曾经写过代码,装上去召回.本文首先召回AsyncTask的基本使用.   AsyncTask说简单点就是 开启一个线程.而且把结果提交给ui线程. Thread+Handler,只 ...

  6. 英文构词法 —— ant、ent 后缀

    1. -ant:--人 accountant:会计, account(ac+count):计数,账户: assistant:助手: assist:帮助 descendant:后裔: descend:下 ...

  7. 用决策树模型求解回归问题(regression tree)

    How do decision trees for regression work? 决策树模型既可以求解分类问题(对应的就是 classification tree),也即对应的目标值是类别型数据, ...

  8. hadoop中国字、词频统计和排序

    例如需求,下面: 有被看作图输入文件中. 代表ip地址,之后的偶数列代表搜索词.数字(奇数列)代表搜索次数.使用"\t"分隔.如今须要对搜索词进行分词并统计词频,此处不考虑搜索次数 ...

  9. Android小游戏:功夫蛇 团队开发经验总结

    前言 曾经没有代码管理的习惯,不用回版本控制工具.这种陋习虽然让原来千穿百孔的代码远离了实现,但这种逃避未必就是一件好事吧;). 于是从博客中挖出了原来的文章,并千辛万苦找到了最早的代码贴出来. 这篇 ...

  10. 编译freetype 的dll

    因需要给python使用freetype库,so需要一个freetype的dll 2 steps 1. 在VC中设置输出为动态链接库 2. 修改ftoption.h 在284行增加2行代码即可 /** ...