檔案系統: 文本的讀寫, 二進位的讀寫, 流的讀寫, 最近訪問列表和未來訪問列表
介紹
重新想象 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; } } }}