Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(23) 文件系统: 文本的读写等

Windows 8 Store Apps学习(23) 文件系统: 文本的读写等2013-12-06 cnblogs webabcd文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表

介绍

重新想象 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;}}}}