热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

重新想象Windows8StoreApps(23)-文件系统:文本的读写,二进制的读写,流的读写,最近访问列表和未来访问列表原文:重新想象Windows8StoreApps(23)
重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表
原文:重新想象 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 &#61; await dataWriter.StoreAsync();await transaction.CommitAsync();lblMsg.Text &#61; "写入成功";}}}private async void btnReadStream_Click_1(object sender, RoutedEventArgs e){// 在指定的目录下获取指定的文件StorageFolder storageFolder &#61; KnownFolders.DocumentsLibrary;StorageFile storageFile &#61; await storageFolder.GetFileAsync("webabcdStream.txt");if (storageFile !&#61; null){using (IRandomAccessStream randomStream &#61; await storageFile.OpenAsync(FileAccessMode.Read)){using (DataReader dataReader &#61; new DataReader(randomStream)){ulong size &#61; randomStream.Size;if (size <&#61; uint.MaxValue){// 获取数据流&#xff0c;从中读取字符串值并显示uint numBytesLoaded &#61; await dataReader.LoadAsync((uint)size);string fileContent &#61; dataReader.ReadString(numBytesLoaded);lblMsg.Text &#61; "读取结果&#xff1a;" &#43; fileContent;}}}}}}
}


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

<Pagex:Class&#61;"XamlDemo.FileSystem.CacheAccess"xmlns&#61;"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x&#61;"http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local&#61;"using:XamlDemo.FileSystem"xmlns:d&#61;"http://schemas.microsoft.com/expression/blend/2008"xmlns:mc&#61;"http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable&#61;"d"><Grid Background&#61;"Transparent"><StackPanel Margin&#61;"120 0 0 0"><TextBlock Name&#61;"lblMsg" FontSize&#61;"14.667" /><Button Name&#61;"btnAddToMostRecentlyUsedList" Content&#61;"AddToMostRecentlyUsedList" Click&#61;"btnAddToMostRecentlyUsedList_Click_1" Margin&#61;"0 10 0 0" /><Button Name&#61;"btnGetMostRecentlyUsedList" Content&#61;"GetMostRecentlyUsedList" Click&#61;"btnGetMostRecentlyUsedList_Click_1" Margin&#61;"0 10 0 0" /><Button Name&#61;"btnAddToFutureAccessList" Content&#61;"AddToFutureAccessList" Click&#61;"btnAddToFutureAccessList_Click_1" Margin&#61;"0 10 0 0" /><Button Name&#61;"btnGetFutureAccessList" Content&#61;"GetFutureAccessList" Click&#61;"btnGetFutureAccessList_Click_1" Margin&#61;"0 10 0 0" />StackPanel>Grid>
Page>

FileSystem/CacheAccess.xaml.cs

/** 演示如何读写“最近访问列表”和“未来访问列表”* 注&#xff1a;如果需要读写某扩展名的文件&#xff0c;需要在 Package.appxmanifest 增加“文件类型关联”声明&#xff0c;并做相应的配置* * StorageFolder - 文件夹操作类* 获取文件夹相关属性、重命名、Create...、Get...等* * StorageFile - 文件操作类* 获取文件相关属性、重命名、Create...、Get...、Copy...、Move...、Delete...、Open...、Replace...等* * StorageApplicationPermissions - 文件/文件夹的访问列表* MostRecentlyUsedList - 最近访问列表&#xff08;实现了 IStorageItemAccessList 接口&#xff09;* Add(IStorageItem file, string metadata) - 添加文件或文件夹到“最近访问列表”&#xff0c;返回 token 值&#xff08;一个字符串类型的标识&#xff09;&#xff0c;通过此值可以方便地检索到对应的文件或文件夹* file - 需要添加到列表的文件或文件夹* metadata - 自定义元数据&#xff0c;相当于上下文* AddOrReplace(string token, IStorageItem file, string metadata) - 添加文件或文件夹到“最近访问列表”&#xff0c;如果已存在则替换* GetFileAsync(string token) - 根据 token 值&#xff0c;在“最近访问列表”查找对应的文件* GetFolderAsync(string token) - 根据 token 值&#xff0c;在“最近访问列表”查找对应的文件夹* GetItemAsync(string token) - 根据 token 值&#xff0c;在“最近访问列表”查找对应的文件或文件夹* Entries - 返回 AccessListEntryView 类型的数据&#xff0c;其是 AccessListEntry 类型数据的集合* FutureAccessList - 未来访问列表&#xff08;实现了 IStorageItemAccessList 接口&#xff09;* 基本用法同“MostRecentlyUsedList”* * AccessListEntry - 用于封装访问列表中的 StorageFile 或 StorageFolder 的 token 和元数据* Token - token 值* Metadata - 元数据* * 注&#xff1a;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 &#61; KnownFolders.DocumentsLibrary;StorageFile storageFile &#61; await storageFolder.CreateFileAsync("webabcdCacheAccess.txt", CreationCollisionOption.ReplaceExisting);// 在指定的文件中写入指定的文本string textContent &#61; "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 &#61; KnownFolders.DocumentsLibrary;StorageFile storageFile &#61; await storageFolder.GetFileAsync("webabcdCacheAccess.txt");if (storageFile !&#61; null){// 将文件添加到“最近访问列表”&#xff0c;并获取对应的 token 值string token &#61; StorageApplicationPermissions.MostRecentlyUsedList.Add(storageFile, storageFile.Name);lblMsg.Text &#61; "token&#xff1a;" &#43; token;}}private async void btnAddToFutureAccessList_Click_1(object sender, RoutedEventArgs e){// 获取文件对象StorageFolder storageFolder &#61; KnownFolders.DocumentsLibrary;StorageFile storageFile &#61; await storageFolder.GetFileAsync("webabcdCacheAccess.txt");if (storageFile !&#61; null){// 将文件添加到“未来访问列表”&#xff0c;并获取对应的 token 值string token &#61; StorageApplicationPermissions.FutureAccessList.Add(storageFile, storageFile.Name);lblMsg.Text &#61; "token&#xff1a;" &#43; token;}}private async void btnGetMostRecentlyUsedList_Click_1(object sender, RoutedEventArgs e){AccessListEntryView entries &#61; StorageApplicationPermissions.MostRecentlyUsedList.Entries;if (entries.Count > 0){// 通过 token 值&#xff0c;从“最近访问列表”中获取文件对象AccessListEntry entry &#61; entries[0];StorageFile storageFile &#61; await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(entry.Token);string textContent &#61; await FileIO.ReadTextAsync(storageFile);lblMsg.Text &#61; "MostRecentlyUsedList 的第一个文件的文本内容&#xff1a;" &#43; textContent;}else{lblMsg.Text &#61; "最近访问列表中无数据";}}private async void btnGetFutureAccessList_Click_1(object sender, RoutedEventArgs e){AccessListEntryView entries &#61; StorageApplicationPermissions.FutureAccessList.Entries;if (entries.Count > 0){// 通过 token 值&#xff0c;从“未来访问列表”中获取文件对象AccessListEntry entry &#61; entries[0];StorageFile storageFile &#61; await StorageApplicationPermissions.FutureAccessList.GetFileAsync(entry.Token);string textContent &#61; await FileIO.ReadTextAsync(storageFile);lblMsg.Text &#61; "FutureAccessList 的第一个文件的文本内容&#xff1a;" &#43; textContent;}else{lblMsg.Text &#61; "未来访问列表中无数据";}}}
}



OK
[源码下载]

posted on 2014-03-08 16:23 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转:https://www.cnblogs.com/lonelyxmas/p/3588112.html



推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • 本文介绍了在Windows环境下如何配置php+apache环境,包括下载php7和apache2.4、安装vc2015运行时环境、启动php7和apache2.4等步骤。希望对需要搭建php7环境的读者有一定的参考价值。摘要长度为169字。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
author-avatar
美美2502909961
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有