WP8开发系列3:WP7与WP8在文件IO操作上的区别 推荐
2012-09-03 09:18
495 查看
引言:微软说明WP7应用程序可以兼容在WP8系统中运行,WP8又与WIN8共享同一个内核,这就意味着WP7时开发的应用可以向上兼容到WP8, WP8开发的应用可以迁移到Win8中, 那么,对于WP7与WP8在文件IO操作上又有哪些区别与共性呢?本篇为您揭晓一二。一、Windows Phone 7的文件操作:如果在现有的wp7开发平台上去写入一个文件,我们会想到使用: IsolatedStorageFile代码如下:
private void WriteFile(string fileName, string content)
{
using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.CreateFile(fileName))
{
using (StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream))
{
streamWriter.Write(content);
}
}
}
}读取一个文件:
private string ReadFile(string fileName)
{
string text;
using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.OpenFile(fileName, FileMode.Open))
{
using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream))
{
text = streamReader.ReadToEnd();
}
}
}
return text;
}然后,可以用IsoStoreSpy这类辅助工具进应用程序查看存储的文件的内容等操作.二、Windows Phone 8 文件操作wp8与win8的文件操作方式类似,如果你在win8下写过文件操作,那么WP8自然熟悉,这需要用到2个接口:IStorageFile和 IStorageFolder, 可以看出,一个是对文件的操作,一个是对目录的。这两个接口均在 :Windwos.Storage组件中,注意:因在windows 8 开发中大量使用了WinRT异步编程方式,故在WP8中编程亦如此。写入文件:
public async Task WriteFile(string fileName, string text)
{
IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream stream = await storageFile.OpenStreamForWriteAsync())
{
byte[] content = Encoding.UTF8.GetBytes(text);
await stream.WriteAsync(content, 0, content.Length);
}
}读取文件:
public async Task<string> ReadFile(string fileName)
{
string text;
IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName);
IRandomAccessStream accessStream = await storageFile.OpenReadAsync();
using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size))
{
byte[] content = new byte[stream.Length];
await stream.ReadAsync(content, 0, (int) stream.Length);
text = Encoding.UTF8.GetString(content, 0, content.Length);
}
return text;
}OK,到此我们将操作文件的方法写好后,就可以在业务层进行调用了,方式如下:
await WriteFile("TestWP8IO.txt", "测试WP8与WP7文件操作");
string text = await ReadFile("TestWP8IO.txt")三、兼容性问题上面的代码分别对WP7与WP8平台的文件写入与读取操作进行了简单的说明,你可能会想:“我在WP7应用中的文件目录结构定义,是不是在WP8中也是一样的呢?”其实,两者是有点区别的,这点区别,也是日后WP7向WP8迁移过程中必须要考虑的元素之一。在WP7中,创建文件userinfo.txt,那么,它在隔离存储区的文件目录结构如下:C:\Data\Users\DefaultAppAccount\AppData\{ProductID}\Local\IsolatedStore\userinfo.txt在WP8中,创建相同文件,文件存储目录结构如下:C:\Data\Users\DefaultAppAccount\AppData\{ProductID}\Local\userinfo.txt两者一对比,我们很直观的就能看到区别在哪里,这就意味着:在WP8开发中,使用现有的WP7代码时,如果你不想改变原有文件目录结构,那你就必须要首先获取WP7隔离存储目录.即:
IStorageFolder applicationFolder =await ApplicationData.Current.LocalFolder.GetFolderAsync("IsolatedStore");最后,想说明的是:如果你想“偷懒”或者说减少开发工作量,就看看我上面说的这些,但我建议各位开发者,最好是全新开发基于WP8平台的应用,毕竟这意味着你也可以在Win8中快速部署。欢迎访问Windows Phone与Win8问答社区:http://ask.metrofeng.com !
相关文章推荐
- WP8开发系列3:WP7与WP8在文件IO操作上的区别
- WP8开发系列3:WP7与WP8在文件IO操作上的区别
- IO操作系列之文件类型获取
- .Net那点事儿系列:System.IO之windows文件操作
- linux下C++开发系列(六)——文件IO相关的系统调用
- .Net那点事儿系列:System.IO之windows文件操作
- ios助手开发系列(六):文件操作系列3 - 文件上传和文件下载
- ios开发系列之文件的写入读取,NSUserDefaults存储数据,NSFileManager操作文件和文件夹
- Android开发学习之IO文件读写操作
- 浅谈无缓存IO操作和标准IO文件操作区别(转载+整理)
- ios助手开发系列(五):文件操作系列2 - 操作文件和文件夹所需要开启的服务:AFC服务和AFC2服务
- ios助手开发系列(四):文件操作系列1 - 获取设备文件信息 AFCFileInfoOpen函数和AFCKeyValueRead函数
- Windows phone开发之文件夹与文件操作系列(一)文件夹与文件操作
- [C# 开发技巧系列] 使用C#操作幻灯片 推荐
- IO文件操作
- Linux下用文件IO的方式操作GPIO(/sys/class/gpio)
- Java EE开发第十二章:1.Servlet之Response相应(重定向、操作相应体、文件下载、随机验证码)
- Android开发之SD卡文件操作分析
- 使用java.io.*操作文件的拆分与合并,Application一例!
- IOS开发-UI学习-NSBundle和NSURL的区别(读取文件以及写入文件)