快速构建Windows 8风格应用28-临时应用数据
2012-11-25 22:20 王祖康 阅读(1293) 评论(0) 编辑 收藏 举报本篇博文主要介绍临时应用数据概览、如何构建临时应用数据。
一、临时应用数据概览
临时应用数据相当于网页中缓存,这些数据文件是不能够漫游的,并且随时可以删除。
通常系统为了维护任务可以随时删除掉这些临时应用数据,同时我们也可以通过“磁盘清理”将这些数据删除掉。
一般我们在应用中存储会话期间的临时信息,例如:QQ的聊天纪录等。
二、如何构建临时应用数据
1、声明临时存储对象
使用ApplicationData.TemporaryFolder属性获取文件。
Windows.Storage.StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
2、将临时数据写入文件
使用Windows.Storage.StorageFolder.CreateFileAsync和Windows.Storage.FileIO.WriteTextAsync在临时应用数据存储中创建和更新文件。
async void WriteTimestamp()
{
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter =
new Windows.Globalization.DatetimeFormatting.DateTimeFormatter("longtime");
StorageFile sampleFile = await temporaryFolder.CreateFileAsync("dataFile.txt",
CreateCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now));
}
3、从文件中获取临时数据
使用Windows.Storage.StorageFolder.GetFileAsync、Windows.Storage.StorageFile.GetFileFromApplicationUriAsync 和 Windows.Storage.FileIO.ReadTextAsync在临时应用数据存储中打开和读取文件。
async void ReadTimestamp()
{
try
{
StorageFile sampleFile = await temporaryFolder.GetFileAsync("dataFile.txt");
String timestamp = await FileIO.ReadTextAsync(sampleFile);
}
catch (Exception)
{
}
}
相关资料可参考:
1.应用数据;
2.应用数据示例;
3.Windows.Storage.ApplicationData;
4.Windows.Storage.ApplicationDataCompositeValue;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!