重新想象 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
<Page
x: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 > )
{
// 通过 token 值,从“最近访问列表”中获取文件对象
AccessListEntry entry = entries[];
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 > )
{
// 通过 token 值,从“未来访问列表”中获取文件对象
AccessListEntry entry = entries[];
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) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表的更多相关文章
- 重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作
原文:重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作 [源码下载 ...
- 重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件
原文:重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件 [源码下载] 重新想象 Windows 8 Store Apps ( ...
- 重新想象 Windows 8 Store Apps 系列文章索引
[源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...
- 重新想象 Windows 8 Store Apps (41) - 打印
[源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...
- 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo
[源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...
- 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解
[源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...
- 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解
[源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...
- 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract
[源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract
[源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...
随机推荐
- 使用CocoaPods出现 The `master` repo requires CocoaPods 0.32.1 - 问题解决
近期在使用CocoaPods为project配置第三方类库时出现了例如以下问题: [!] The `master` repo requires CocoaPods 0.32.1 - 明显是由于Coco ...
- spring中bean的设计模式
默认的是单例的. 如果不想单例需要如下配置: <bean id="user" class="..." singleton="false" ...
- codeforces 577
codeforces 577A 题目链接:http://codeforces.com/problemset/problem/577/A 题目大意:给出一个n*n的表格,每个表格对应的值为横坐标*纵坐标 ...
- 深入探讨:LBS是一种工具而非一种模式
移动互联网的快速发展,带动着移动互联网应用的不断创新.从2010起,LBS的概念就在中国迅速兴起,但到了2011年底提供LBS服务的企业从最多50家已经降至仅剩15家.投行在看好移动互联网的同时又对L ...
- OCA读书笔记(17) - 移动数据
Sql*load 1. sql*loader的文件有哪些? 日志文件:概述了作业的成功与失败以及所有相关错误的细节 错误文件(bad file):从输入文件中抽取的行可能会被sqlldr丢弃(原因可能 ...
- C#函数参数传递解惑
C#语言函数参数的传递 就像C语言众多的后世子孙一样,C#的函数参数是非常讲究的.首先,参数必须写在函数名后面的括号里,这里我们有必要称其为形参.参数必须有一个参数名称和明确的类型声明.该参数名称 ...
- Coreseek:indexer crashed神秘
浩哥前两天让我再Coreseek该指数再次这样做,由于需求方面变化不大,公司名称应出现指数.在添加的配置文件的面孔sql_field_string:串场.. 此属性特别有用,因为它不仅作为过滤器的特性 ...
- RPC分布式处理
RPC(远程过程调用)的应用 接触背景 因为工作上某项目的需要设计一种分布式处理耗时的运算,每个节点然后将运算结果返回给中心服务器,而最初未了解RPC这部分之前我的设计是在每一个RPC服务器上搭建一个 ...
- sql语句中 limi的用法
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset 使用查询语句时需要返回前几条或者中间的某几行数据时可以用到limit 例如 ...
- openwrt教程 第一章 物联网&openwrt开发概述
1.1 我们的宗旨 互联网.移动互联网的时代已经过去,物联网的时代已经来临!2014年,是物联网元年,2016年,物联网将达到高潮!为了迎接该潮流,我们工作室(F403科技创意室:http://f40 ...