Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(22) 文件系统: 访问文件夹和文件,搜索本地文件

Windows 8 Store Apps学习(22) 文件系统: 访问文件夹和文件,搜索本地文件2013-12-06 cnblogs webabcd文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件

介绍

重新想象 Windows 8 Store Apps 之 文件系统

File Access - 访问文件夹和文件,以及获取文件的各种属性

Folder Access - 遍历文件夹时的一些特殊操作

Thumbnail Access - 获取文件的缩略图

AQS - 通过 AQS(Advanced Query Syntax)搜索本地文件

示例

1、演示如何访问文件夹和文件,以及如何获取文件的各种属性

FileSystem/FileAccess.xaml

<Pagex: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

<Pagex: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>