Windows 8 Store Apps學習(22) 檔案系統: 訪問檔案夾和檔案,搜尋本地檔案

來源:互聯網
上載者:User

檔案系統: 訪問檔案夾和檔案, 通過 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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.