重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件
原文:重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件
作者:webabcd
介绍
重新想象 Windows 8 Store Apps 之 文件系统
- File Access - 访问文件夹和文件,以及获取文件的各种属性
- Folder Access - 遍历文件夹时的一些特殊操作
- Thumbnail Access - 获取文件的缩略图
- AQS - 通过 AQS(Advanced Query Syntax)搜索本地文件
示例
1、演示如何访问文件夹和文件,以及如何获取文件的各种属性
FileSystem/FileAccess.xaml
<Page
x:Class="XamlDemo.FileSystem.FileAccess"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.FileSystem"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <ListBox Name="listBox" Width="400" Height="200" SelectionChanged="listBox_SelectionChanged_1" HorizontalAlignment="Left" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>
FileSystem/FileAccess.xaml.cs
/*
* 演示如何访问文件夹和文件,以及如何获取文件的各种属性
*
* StorageFolder - 文件夹操作类
* 获取文件夹相关属性、重命名、Create...、Get...等
*
* StorageFile - 文件操作类
* 获取文件相关属性、重命名、Create...、Get...、Copy...、Move...、Delete...、Open...、Replace...等
*
* 注:WinRT 中的关于存储操作的相关类都在 Windows.Storage 命名空间内
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using System.Linq; namespace XamlDemo.FileSystem
{
public sealed partial class FileAccess : Page
{
public FileAccess()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
// 遍历“文档库”目录下的所有顶级文件(需要在 Package.appxmanifest 中选中“文档库”功能)
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
IReadOnlyList<StorageFile> files = await storageFolder.GetFilesAsync();
listBox.ItemsSource = files.Select(p => p.Name).ToList();
} private async void listBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
// 获取用户选中的文件
string fileName = (string)listBox.SelectedItem;
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile storageFile = await storageFolder.GetFileAsync(fileName); // 显示文件的各种属性
if (storageFile != null)
{
lblMsg.Text = "Name:" + storageFile.Name;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "FileType:" + storageFile.FileType;
lblMsg.Text += Environment.NewLine; BasicProperties basicProperties = await storageFile.GetBasicPropertiesAsync();
lblMsg.Text += "Size:" + basicProperties.Size;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DateModified:" + basicProperties.DateModified;
lblMsg.Text += Environment.NewLine; /*
* 获取文件的其它各种属性
* 详细属性列表请参见:http://msdn.microsoft.com/en-us/library/windows/desktop/ff521735(v=vs.85).aspx
*/
List<string> propertiesName = new List<string>();
propertiesName.Add("System.DateAccessed");
propertiesName.Add("System.DateCreated");
propertiesName.Add("System.FileOwner");
IDictionary<string, object> extraProperties = await storageFile.Properties.RetrievePropertiesAsync(propertiesName); lblMsg.Text += "System.DateAccessed:" + extraProperties["System.DateAccessed"];
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "System.DateCreated:" + extraProperties["System.DateCreated"];
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "System.FileOwner:" + extraProperties["System.FileOwner"];
}
}
}
}
2、演示遍历文件夹时的一些特殊操作
FileSystem/FolderAccess.xaml
<Page
x:Class="XamlDemo.FileSystem.FolderAccess"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.FileSystem"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnGroupFile" Content="分组文件" Click="btnGroupFile_Click_1" Margin="0 10 0 0" /> <Button Name="btnPrefetchAPI" Content="从 Prefetch 中加载信息" Click="btnPrefetchAPI_Click_1" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>
FileSystem/FolderAccess.xaml.cs
/*
* 演示遍历文件夹时的一些特殊操作
* 1、演示如何对 StorageFolder 中的内容做分组
* 2、演示如何通过文件扩展名过滤内容,以及如何从 Prefetch 中获取数据
*
* StorageFolder - 文件夹操作类
* 获取文件夹相关属性、重命名、Create...、Get...等
*
* StorageFile - 文件操作类
* 获取文件相关属性、重命名、Create...、Get...、Copy...、Move...、Delete...、Open...、Replace...等
*
* 注:WinRT 中的关于存储操作的相关类都在 Windows.Storage 命名空间内
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Search;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.FileSystem
{
public sealed partial class FolderAccess : Page
{
public FolderAccess()
{
this.InitializeComponent();
} // 演示如何对 StorageFolder 中的内容做分组
private async void btnGroupFile_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
lblMsg.Text = ""; StorageFolder picturesFolder = KnownFolders.PicturesLibrary; // 创建一个按月份分组的查询
QueryOptions queryOptions = new QueryOptions(CommonFolderQuery.GroupByMonth);
// 对指定文件夹执行指定的文件夹查询,返回分组后的文件夹数据
StorageFolderQueryResult queryResult = picturesFolder.CreateFolderQueryWithOptions(queryOptions); IReadOnlyList<StorageFolder> folderList = await queryResult.GetFoldersAsync();
foreach (StorageFolder folder in folderList) // 这里的 floder 就是按月份分组后的月份文件夹(当然,物理上并没有月份文件夹)
{
IReadOnlyList<StorageFile> fileList = await folder.GetFilesAsync();
lblMsg.Text += folder.Name + " (" + fileList.Count + ")";
lblMsg.Text += Environment.NewLine;
foreach (StorageFile file in fileList) // 月份文件夹内的文件
{
lblMsg.Text += " " + file.Name;
lblMsg.Text += Environment.NewLine;
}
}
} // 演示如何通过文件扩展名过滤内容,以及如何从 Prefetch 中获取数据
private async void btnPrefetchAPI_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
/*
* Prefetch 是预读的文件信息,其在 C:\Windows\Prefetch 目录内,可以从中获取文件属性信息和文件缩略图,在访问大量文件的场景下可以提高效率
*/ lblMsg.Text = ""; List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".jpg");
fileTypeFilter.Add(".png"); // 新建一个查询,指定需要查询的文件的扩展名
QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter); // 通过 QueryOptions.SetPropertyPrefetch() 来设置需要从 Prefetch 中获取的属性信息
List<string> propertyNames = new List<string>();
propertyNames.Add("System.FileOwner");
queryOptions.SetPropertyPrefetch(PropertyPrefetchOptions.ImageProperties, propertyNames); /*
* 通过 QueryOptions.SetThumbnailPrefetch() 来设置需要从 Prefetch 中获取的缩略图
uint requestedSize = 190;
ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView;
ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale;
queryOptions.SetThumbnailPrefetch(thumbnailMode, requestedSize, thumbnailOptions);
*/ // 对指定文件夹执行指定的文件查询,返回查询后的文件数据
StorageFileQueryResult queryResult = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions); IReadOnlyList<StorageFile> fileList = await queryResult.GetFilesAsync();
foreach (StorageFile file in fileList)
{
lblMsg.Text += file.Name; // 文件名 // 获取图像属性
ImageProperties properties = await file.Properties.GetImagePropertiesAsync();
lblMsg.Text += "(" + properties.Width + "x" + properties.Height + ")"; // 获取其他属性(详细属性列表请参见:http://msdn.microsoft.com/en-us/library/windows/desktop/ff521735(v=vs.85).aspx)
IDictionary<string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertyNames);
lblMsg.Text += "(" + extraProperties["System.FileOwner"] + ")"; // 获取缩略图
// StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);
}
}
}
}
3、演示如何获取文件的缩略图
FileSystem/ThumbnailAccess.xaml
<Page
x:Class="XamlDemo.FileSystem.ThumbnailAccess"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.FileSystem"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<ScrollViewer Margin="120 0 0 0">
<StackPanel> <Button Name="btnGetThumbnail" Content="获取文件的缩略图" Click="btnGetThumbnail_Click_1" /> <Image Name="img" Stretch="None" HorizontalAlignment="Left" Margin="0 10 0 0" />
<TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> </StackPanel>
</ScrollViewer>
</Grid>
</Page>
FileSystem/ThumbnailAccess.xaml.cs
/*
* 演示如何获取文件的缩略图
*
* 获取指定文件或文件夹的缩略图,返回 StorageItemThumbnail 类型的数据
* StorageFile.GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options)
* StorageFolder.GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options)
* ThumbnailMode mode - 用于描述缩略图的目的,以使系统确定缩略图图像的调整方式(PicturesView, VideosView, MusicView, DocumentsView, ListView, SingleItem)
* 关于 ThumbnailMode 的详细介绍参见:http://msdn.microsoft.com/en-us/library/windows/apps/hh465350.aspx
* uint requestedSize - 期望尺寸的最长边长的大小
* ThumbnailOptions options - 检索和调整缩略图的行为(None, ReturnOnlyIfCached, ResizeThumbnail, UseCurrentScale)
*/ using System;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging; namespace XamlDemo.FileSystem
{
public sealed partial class ThumbnailAccess : Page
{
public ThumbnailAccess()
{
this.InitializeComponent();
} private async void btnGetThumbnail_Click_1(object sender, RoutedEventArgs e)
{
if (XamlDemo.Common.Helper.EnsureUnsnapped())
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add("*"); StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView;
ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale;
uint requestedSize = ; using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions))
{
if (thumbnail != null)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(thumbnail);
img.Source = bitmapImage; lblMsg.Text = "file name: " + file.Name;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "requested size: " + requestedSize;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "returned size: " + thumbnail.OriginalWidth + "*" + thumbnail.OriginalHeight;
}
}
}
}
}
}
}
4、演示如何通过 AQS - Advanced Query Syntax 搜索本地文件
FileSystem/AQS.xaml.cs
/*
* 演示如何通过 AQS - Advanced Query Syntax 搜索本地文件
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.BulkAccess;
using Windows.Storage.FileProperties;
using Windows.Storage.Search;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.FileSystem
{
public sealed partial class AQS : Page
{
public AQS()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
// 准备在“音乐库”中进行搜索(需要在 Package.appxmanifest 的“功能”中选中“音乐库”)
StorageFolder musicFolder = KnownFolders.MusicLibrary; // 准备搜索所有类型的文件
List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add("*"); // 搜索的查询参数
QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByDate, fileTypeFilter);
// 指定 AQS 字符串,参见 http://msdn.microsoft.com/zh-cn/library/windows/apps/aa965711.aspx
queryOptions.UserSearchFilter = "五月天"; // 根据指定的参数创建一个查询
StorageFileQueryResult fileQuery = musicFolder.CreateFileQueryWithOptions(queryOptions); lblMsg.Text = "在音乐库中搜索“五月天”,结果如下:";
lblMsg.Text += Environment.NewLine; // 开始搜索,并返回检索到的文件列表
IReadOnlyList<StorageFile> files = await fileQuery.GetFilesAsync(); if (files.Count == )
{
lblMsg.Text += "什么都没搜到";
}
else
{
foreach (StorageFile file in files)
{
lblMsg.Text += file.Name;
lblMsg.Text += Environment.NewLine;
}
} // 关于 QueryOptions 的一些用法,更详细的 QueryOptions 的说明请参见 msdn
queryOptions = new QueryOptions();
queryOptions.FolderDepth = FolderDepth.Deep;
queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;
queryOptions.SortOrder.Clear();
var sortEntry = new SortEntry();
sortEntry.PropertyName = "System.FileName";
sortEntry.AscendingOrder = true;
queryOptions.SortOrder.Add(sortEntry); fileQuery = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions);
}
}
}
OK
[源码下载]
重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件的更多相关文章
- 重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作
原文:重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作 [源码下载 ...
- 重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表
原文:重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表 [源码下载] 重新想象 Windows 8 S ...
- 重新想象 Windows 8 Store Apps 系列文章索引
[源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...
- 重新想象 Windows 8 Store Apps (41) - 打印
[源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...
- 重新想象 Windows 8 Store Apps (20) - 动画: ThemeAnimation(主题动画)
原文:重新想象 Windows 8 Store Apps (20) - 动画: ThemeAnimation(主题动画) [源码下载] 重新想象 Windows 8 Store Apps (20) - ...
- 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo
[源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...
- 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解
[源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...
- 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解
[源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...
- 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract
[源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
随机推荐
- php获取server端mac和clientmac的地址
获取servermac <?php /** 获取网卡的MAC地址原码:眼下支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetmacAddr{ var $re ...
- 面向接口可扩展框架之“Mvc扩展框架及DI”
面向接口可扩展框架之“Mvc扩展框架及DI” 标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把 ...
- java学习笔记11--Annotation
java学习笔记11--Annotation Annotation:在JDK1.5之后增加的一个新特性,这种特性被称为元数据特性,在JDK1.5之后称为注释,即:使用注释的方式加入一些程序的信息. j ...
- wkhtmtopdf--高分辨率转HTML成PDF--目录篇
原文:wkhtmtopdf--高分辨率转HTML成PDF--目录篇 wkhtmtopdf--高分辨率转HTML成PDF(一):简述wkhtmtopdf的简介和安装 wkhtmtopdf--高分辨率转H ...
- csdn肿么了,这两天写的博文都是待审核
昨天早上8点写了一篇博文,然后点击发表,结果系统显示"待审核".于是仅仅好qq联系csdn的客服,等到9点时候,csdn的客服上线了,然后回复说是链接达到5个以上须要审核,于是回到 ...
- windows 7多点触摸开发
win7 触摸屏系统应用广泛,软件操作方便,功能强大,现以被很多硬件厂商应用. 我曾用一台装有win7 的汉王平板电脑进行了多点触摸软件的开发. 开发环境及条件: 1. 平板电脑+ win7 ...
- HDU 5009 Paint Pearls (动态规划)
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...
- Cocos2d-x v3.0正式版尝鲜体验【3】 Label文本标签
Cocos2d-x在新版本号中增加了新的Label API.和以往不同的是,2.x的版本号是通过三个不同的类来创建不同的文本标签,而如今是模仿着精灵的创建方式.一个类创建不同形式的文本,只是核心内容还 ...
- HDU 4454 - Stealing a Cake(三分)
我比较快速的想到了三分,但是我是从0到2*pi区间进行三分,并且漏了一种点到边距离的情况,一直WA了好几次 后来画了下图才发现,0到2*pi区间内是有两个极值的,每个半圆存在一个极值 以下是代码 #i ...
- 编程算法 - 字典分词 代码(C)
字典分词 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 给定字典, 给定一句话, 进行分词. 使用深度遍历(DFS)的方法. 使用一个參数string ...