Windows 8 Store Apps學習(23) 檔案系統: 文本的讀寫等

來源:互聯網
上載者:User

檔案系統: 文本的讀寫, 二進位的讀寫, 流的讀寫, 最近訪問列表和未來訪問列表

介紹

重新想象 Windows 8 Store Apps 之 檔案系統

示範如何讀寫文本資料

示範如何讀寫位元據

示範如何讀寫流資料

示範如何讀寫“最近訪問列表”和“未來訪問列表”

樣本

1、示範如何讀寫文本資料

FileSystem/ReadWriteText.xaml.cs

/* * 示範如何讀寫文本資料 * 註:如果需要讀寫某副檔名的檔案,需要在 Package.appxmanifest 增加“檔案類型關聯”聲明,並做相應的配置 *  * StorageFolder - 檔案夾操作類 *     擷取檔案夾相關屬性、重新命名、Create...、Get...等 *  * StorageFile - 檔案操作類 *     擷取檔案相關屬性、重新命名、Create...、Get...、Copy...、Move...、Delete...、Open...、Replace...等 *      * FileIO - 用於讀寫 IStorageFile 對象的協助類 *     WriteTextAsync() - 將指定的文本資料寫入到指定的檔案 *     AppendTextAsync() - 將指定的文本資料追加到指定的檔案 *     WriteLinesAsync() - 將指定的多行文本資料寫入到指定的檔案 *     AppendLinesAsync() - 將指定的多行文本資料追加到指定的檔案 *     ReadTextAsync() - 擷取指定的檔案中的文本資料 *     ReadLinesAsync() - 擷取指定的檔案中的文本資料,返回的是一行一行的資料 *      * 註:WinRT 中的關於儲存操作的相關類都在 Windows.Storage 命名空間內 */    using System;using Windows.Storage;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;    namespace XamlDemo.FileSystem{    public sealed partial class ReadWriteText : Page    {        public ReadWriteText()        {            this.InitializeComponent();        }        private async void btnWriteText_Click_1(object sender, RoutedEventArgs e)        {            // 在指定的目錄下建立指定的檔案            StorageFolder storageFolder = KnownFolders.DocumentsLibrary;            StorageFile storageFile = await storageFolder.CreateFileAsync("webabcdText.txt", CreationCollisionOption.ReplaceExisting);                // 在指定的檔案中寫入指定的文本            string textContent = "I am webabcd";            await FileIO.WriteTextAsync(storageFile, textContent, Windows.Storage.Streams.UnicodeEncoding.Utf8);                lblMsg.Text = "寫入成功";        }            private async void btnReadText_Click_1(object sender, RoutedEventArgs e)        {            // 在指定的目錄下擷取指定的檔案            StorageFolder storageFolder = KnownFolders.DocumentsLibrary;            StorageFile storageFile = await storageFolder.GetFileAsync("webabcdText.txt");                if (storageFile != null)            {                // 擷取指定的檔案中的常值內容                string textContent = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8);                lblMsg.Text = "讀取結果:" + textContent;            }        }    }}

2、示範如何讀寫位元據

FileSystem/ReadWriteBinary.xaml.cs

/* * 示範如何讀寫位元據 * 註:如果需要讀寫某副檔名的檔案,需要在 Package.appxmanifest 增加“檔案類型關聯”聲明,並做相應的配置 *  * StorageFolder - 檔案夾操作類 *     擷取檔案夾相關屬性、重新命名、Create...、Get...等 *  * StorageFile - 檔案操作類 *     擷取檔案相關屬性、重新命名、Create...、Get...、Copy...、Move...、Delete...、Open...、Replace...等 *      * FileIO - 用於讀寫 IStorageFile 對象的協助類 *     WriteBufferAsync() - 將指定的位元據寫入指定的檔案 *     ReadBufferAsync() - 擷取指定的檔案中的位元據 *      * IBuffer - WinRT 中的位元組數組 *      * 註:WinRT 中的關於儲存操作的相關類都在 Windows.Storage 命名空間內 */    using System;using Windows.Storage;using Windows.Storage.Streams;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;    namespace XamlDemo.FileSystem{    public sealed partial class ReadWriteBinary : Page    {        public ReadWriteBinary()        {            this.InitializeComponent();        }            private async void btnWriteBinary_Click_1(object sender, RoutedEventArgs e)        {            // 在指定的目錄下建立指定的檔案            StorageFolder storageFolder = KnownFolders.DocumentsLibrary;            StorageFile storageFile = await storageFolder.CreateFileAsync("webabcdBinary.txt", CreationCollisionOption.ReplaceExisting);                // 將字串轉換成位元據,並儲存到指定檔案            string textContent = "I am webabcd";            IBuffer buffer = ConverterHelper.String2Buffer(textContent);            await FileIO.WriteBufferAsync(storageFile, buffer);                lblMsg.Text = "寫入成功";        }            private async void btnReadBinary_Click_1(object sender, RoutedEventArgs e)        {            // 在指定的目錄下擷取指定的檔案            StorageFolder storageFolder = KnownFolders.DocumentsLibrary;            StorageFile storageFile = await storageFolder.GetFileAsync("webabcdBinary.txt");                if (storageFile != null)            {                // 擷取指定檔案中的位元據,將其轉換成字串並顯示                IBuffer buffer = await FileIO.ReadBufferAsync(storageFile);                string textContent = ConverterHelper.Buffer2String(buffer);                    lblMsg.Text = "讀取結果:" + textContent;            }        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.