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);
}
}
最新文章
- JS中的decodeURIComponent和encodeURIComponent
- Spark:读取hdfs gz压缩包
- 检测Java程序运行时间的2种方法(高精度的时间[纳秒]与低精度的时间[毫秒])
- bmob
- pl/sql乱码
- android学习笔记43——图形图像处理3——Path
- 解决Git报错:The current branch is not configured for pull No value for key branch.master.merge found in configuration
- 对C语言中va_list,va_start,va_arg和va_end的一点理解
- 基于ASP.Net +easyUI框架上传图片,实现图片上传,提交表单
- 电网SVG简介
- IOS,Object C学习过程中遇到的attributes
- C#计算某个时间距离当前日期的天数
- 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西
- mysql必知必会系列(一)
- HDU-1171 Big Event in HDU(生成函数/背包dp)
- Python基础——1基础
- mac php7.0~7.2 memcache安装
- sql对于字符串的处理
- Maya 常用环境变量详解
- Django之Web框架本质及第一个Django实例
热门文章
- mysqlbinlog命令使用
- Everything starts with a dream(A day has only 24 hours and these things take time,所以要抓紧)
- Lucene + Pinyin4J 提供首字母搜索(——)
- Xcode7 不能使用http网络请求问题解决
- Mac修改文件权限:You don’t have permission to save the file
- activity-alias详解及应用
- 【t045】细菌
- C# WinForm开发系列 - Report
- 一种基于uCos-II操作系统和lwIP协议栈的IEEE-1588主站以及基于该主站的报文处理方法
- Windows 7 X64位平台下,VC6调试运行程序,中断调试无法退出