檔案系統: 訪問檔案夾和檔案, 通過 AQS 搜尋本地檔案
介紹
重新想象 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>