原文:重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表

[源码下载]

重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表

作者: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;}}}
}

3、演示如何读写流数据
FileSystem/ReadWriteStream.xaml.cs

/** 演示如何读写流数据* 注:如果需要读写某扩展名的文件,需要在 Package.appxmanifest 增加“文件类型关联”声明,并做相应的配置* * StorageFolder - 文件夹操作类*     获取文件夹相关属性、重命名、Create...、Get...等* * StorageFile - 文件操作类*     获取文件相关属性、重命名、Create...、Get...、Copy...、Move...、Delete...、Open...、Replace...等*     * IBuffer - WinRT 中的字节数组* * IInputStream - 需要读取的流* IOutputStream - 需要写入的流* IRandomAccessStream - 需要读取、写入的流,其继承自 IInputStream 和 IOutputStream* * DataReader - 从数据流中读取数据,即从 IInputStream 读取*     LoadAsync() - 从数据流中加载指定长度的数据到缓冲区*     ReadInt32(), ReadByte(), ReadString() 等 - 从缓冲区中读取数据* DataWriter - 将数据写入数据流,即写入 IOutputStream*     WriteInt32(), WriteByte(), WriteString() 等 - 将数据写入缓冲区*     StoreAsync() - 将缓冲区中的数据保存到数据流* * StorageStreamTransaction - 用于写数据流到文件的类(具体用法,详见下面的代码)*     Stream - 数据流(只读)*     CommitAsync - 将数据流保存到文件*     * 注: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 ReadWriteStream : Page{public ReadWriteStream(){this.InitializeComponent();}private async void btnWriteStream_Click_1(object sender, RoutedEventArgs e){// 在指定的目录下创建指定的文件StorageFolder storageFolder = KnownFolders.DocumentsLibrary;StorageFile storageFile = await storageFolder.CreateFileAsync("webabcdStream.txt", CreationCollisionOption.ReplaceExisting);string textContent = "I am webabcd";using (StorageStreamTransaction transaction = await storageFile.OpenTransactedWriteAsync()){using (DataWriter dataWriter = new DataWriter(transaction.Stream)){// 将字符串写入数据流,然后将数据流保存到文件
                    dataWriter.WriteString(textContent);transaction.Stream.Size = await dataWriter.StoreAsync();await transaction.CommitAsync();lblMsg.Text = "写入成功";}}}private async void btnReadStream_Click_1(object sender, RoutedEventArgs e){// 在指定的目录下获取指定的文件StorageFolder storageFolder = KnownFolders.DocumentsLibrary;StorageFile storageFile = await storageFolder.GetFileAsync("webabcdStream.txt");if (storageFile != null){using (IRandomAccessStream randomStream = await storageFile.OpenAsync(FileAccessMode.Read)){using (DataReader dataReader = new DataReader(randomStream)){ulong size = randomStream.Size;if (size <= uint.MaxValue){// 获取数据流,从中读取字符串值并显示uint numBytesLoaded = await dataReader.LoadAsync((uint)size);string fileContent = dataReader.ReadString(numBytesLoaded);lblMsg.Text = "读取结果:" + fileContent;}}}}}}
}

4、演示如何读写“最近访问列表”和“未来访问列表”
FileSystem/CacheAccess.xaml

<Pagex:Class="XamlDemo.FileSystem.CacheAccess"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="btnAddToMostRecentlyUsedList" Content="AddToMostRecentlyUsedList" Click="btnAddToMostRecentlyUsedList_Click_1" Margin="0 10 0 0" /><Button Name="btnGetMostRecentlyUsedList" Content="GetMostRecentlyUsedList" Click="btnGetMostRecentlyUsedList_Click_1" Margin="0 10 0 0" /><Button Name="btnAddToFutureAccessList" Content="AddToFutureAccessList" Click="btnAddToFutureAccessList_Click_1" Margin="0 10 0 0" /><Button Name="btnGetFutureAccessList" Content="GetFutureAccessList" Click="btnGetFutureAccessList_Click_1" Margin="0 10 0 0" /></StackPanel></Grid>
</Page>

FileSystem/CacheAccess.xaml.cs

/** 演示如何读写“最近访问列表”和“未来访问列表”* 注:如果需要读写某扩展名的文件,需要在 Package.appxmanifest 增加“文件类型关联”声明,并做相应的配置* * StorageFolder - 文件夹操作类*     获取文件夹相关属性、重命名、Create...、Get...等* * StorageFile - 文件操作类*     获取文件相关属性、重命名、Create...、Get...、Copy...、Move...、Delete...、Open...、Replace...等*     * StorageApplicationPermissions - 文件/文件夹的访问列表*     MostRecentlyUsedList - 最近访问列表(实现了 IStorageItemAccessList 接口)*         Add(IStorageItem file, string metadata) - 添加文件或文件夹到“最近访问列表”,返回 token 值(一个字符串类型的标识),通过此值可以方便地检索到对应的文件或文件夹*             file - 需要添加到列表的文件或文件夹*             metadata - 自定义元数据,相当于上下文*         AddOrReplace(string token, IStorageItem file, string metadata) - 添加文件或文件夹到“最近访问列表”,如果已存在则替换*         GetFileAsync(string token) - 根据 token 值,在“最近访问列表”查找对应的文件*         GetFolderAsync(string token) - 根据 token 值,在“最近访问列表”查找对应的文件夹*         GetItemAsync(string token) - 根据 token 值,在“最近访问列表”查找对应的文件或文件夹*         Entries - 返回 AccessListEntryView 类型的数据,其是 AccessListEntry 类型数据的集合*     FutureAccessList - 未来访问列表(实现了 IStorageItemAccessList 接口)*         基本用法同“MostRecentlyUsedList”*         * AccessListEntry - 用于封装访问列表中的 StorageFile 或 StorageFolder 的 token 和元数据*     Token - token 值*     Metadata - 元数据*     * 注:WinRT 中的关于存储操作的相关类都在 Windows.Storage 命名空间内*/using System;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;namespace XamlDemo.FileSystem
{public sealed partial class CacheAccess : Page{public CacheAccess(){this.InitializeComponent();}protected async override void OnNavigatedTo(NavigationEventArgs e){// 在指定的目录下创建指定的文件StorageFolder storageFolder = KnownFolders.DocumentsLibrary;StorageFile storageFile = await storageFolder.CreateFileAsync("webabcdCacheAccess.txt", CreationCollisionOption.ReplaceExisting);// 在指定的文件中写入指定的文本string textContent = "I am webabcd";await FileIO.WriteTextAsync(storageFile, textContent, Windows.Storage.Streams.UnicodeEncoding.Utf8);}private async void btnAddToMostRecentlyUsedList_Click_1(object sender, RoutedEventArgs e){// 获取文件对象StorageFolder storageFolder = KnownFolders.DocumentsLibrary;StorageFile storageFile = await storageFolder.GetFileAsync("webabcdCacheAccess.txt");if (storageFile != null){// 将文件添加到“最近访问列表”,并获取对应的 token 值string token = StorageApplicationPermissions.MostRecentlyUsedList.Add(storageFile, storageFile.Name);lblMsg.Text = "token:" + token;}}private async void btnAddToFutureAccessList_Click_1(object sender, RoutedEventArgs e){// 获取文件对象StorageFolder storageFolder = KnownFolders.DocumentsLibrary;StorageFile storageFile = await storageFolder.GetFileAsync("webabcdCacheAccess.txt");if (storageFile != null){// 将文件添加到“未来访问列表”,并获取对应的 token 值string token = StorageApplicationPermissions.FutureAccessList.Add(storageFile, storageFile.Name);lblMsg.Text = "token:" + token;}}private async void btnGetMostRecentlyUsedList_Click_1(object sender, RoutedEventArgs e){AccessListEntryView entries = StorageApplicationPermissions.MostRecentlyUsedList.Entries;if (entries.Count > 0){// 通过 token 值,从“最近访问列表”中获取文件对象AccessListEntry entry = entries[0];StorageFile storageFile = await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(entry.Token);string textContent = await FileIO.ReadTextAsync(storageFile);lblMsg.Text = "MostRecentlyUsedList 的第一个文件的文本内容:" + textContent;}else{lblMsg.Text = "最近访问列表中无数据";}}private async void btnGetFutureAccessList_Click_1(object sender, RoutedEventArgs e){AccessListEntryView entries = StorageApplicationPermissions.FutureAccessList.Entries;if (entries.Count > 0){// 通过 token 值,从“未来访问列表”中获取文件对象AccessListEntry entry = entries[0];StorageFile storageFile = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(entry.Token);string textContent = await FileIO.ReadTextAsync(storageFile);lblMsg.Text = "FutureAccessList 的第一个文件的文本内容:" + textContent;}else{lblMsg.Text = "未来访问列表中无数据";}}}
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表...相关推荐

  1. 重新想象 Windows 8 Store Apps (61) - 通信: http, oauth

    重新想象 Windows 8 Store Apps (61) - 通信: http, oauth 原文:重新想象 Windows 8 Store Apps (61) - 通信: http, oauth ...

  2. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom...

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

  3. 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop...

    重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 原文:重新想象 Window ...

  4. 重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口...

    原文:重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口 [源码下载] 重新想象 Windows 8 Store Apps (27) - ...

  5. 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换...

    重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换 原文: ...

  6. 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础

    原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...

  7. 重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider...

    重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider 原文:重新想象 Windows 8 S ...

  8. 重新想象 Windows 8 Store Apps (59) - 锁屏

    原文:重新想象 Windows 8 Store Apps (59) - 锁屏 [源码下载] 重新想象 Windows 8 Store Apps (59) - 锁屏 作者:webabcd 介绍 重新想象 ...

  9. Unity Game Starter Kit for Windows Store and Windows Phone Store games

    原地址:http://digitalerr0r.wordpress.com/2013/09/30/unity-game-starter-kit-for-windows-store-and-window ...

  10. 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame

    重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame 原文:重新想象 Windows 8.1 Store Apps (79) - 控 ...

最新文章

  1. 机器学习的优化目标、期望最大化(Expectation-Maximum, EM)算法、期望最大化(EM)和梯度下降对比
  2. JavaScript 二进制的 AST
  3. 18.自定义过滤器表头排序
  4. SpringBoot与quartz框架实现分布式定时任务
  5. memcached全面剖析–2.理解 memcached的内存存储
  6. java年利润编程题_[编程入门]利润计算-题解(Java代码)
  7. 华为交换机配置syslog发送_华为/H3C Syslog配置
  8. 什么样的产品可以成功?
  9. 防止表格中的单行按钮被频繁点击,前端实例讲解~
  10. 顶尖的语音识别软件――Nuance Recognizer_语音识别_CTI论坛
  11. opencv 光线影响_在OpenCV中使用色彩校正
  12. JSP中的坑(一):一个空格都不能少
  13. 解决办法:E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用)
  14. CIA:要破解最新iPhone/iOS我们也没辙
  15. 小米6显示服务器出错,小米6解锁BL显示未连接手机解决办法以及各种小技巧汇总......
  16. 区分LJMP、AJMP、SJMP、JMP指令
  17. 酒桌上的规矩,社会潜规则
  18. 全国DNS服务器IP地址【电信、网通、铁通】。
  19. android的listview分组显示的时候layout_marginTop失效的解决办法
  20. FastqC结果简介

热门文章

  1. 动态链接库dll生成与调用 加密 电脑唯一识别 windows下多个cmd命令输出结果的同时获取 本地时间的处理
  2. 刷题记录 kuangbin带你飞专题五:并查集
  3. jq 获取父元素html,jq获取父级元素_使用jquery获取父元素或父节点的方法
  4. 九章算法班L6 Graph Search
  5. 【游戏体验】Colour My World(让我的世界充满色彩)
  6. onclick获取当前节点
  7. zoj 3599 Game 博弈论
  8. Linux命令行下播放音乐SOX
  9. java系统排序_java各种排序实现
  10. 【Spring-tx】spring事务和mybatis的联系