您的位置:首页 > 其它

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 !
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息