WP8开发系列3:WP7与WP8在文件IO操作上的区别
2012-10-11 16:04
537 查看
引言:
微软说明WP7应用程序可以兼容在WP8系统中运行,WP8又与WIN8共享同一个内核,这就意味着WP7时开发的应用可以向上兼容到WP8,WP8开发的应用可以迁移到Win8中,那么,对于WP7与WP8在文件IO操作上又有哪些区别与共性呢?本篇为您揭晓一二。
一、WindowsPhone7的文件操作:
如果在现有的wp7开发平台上去写入一个文件,我们会想到使用:IsolatedStorageFile
代码如下:
privatevoidWriteFile(stringfileName,stringcontent)
[code]{
using(IsolatedStorageFileisolatedStorageFile=IsolatedStorageFile.GetUserStoreForApplication())
{
using(IsolatedStorageFileStreamisolatedStorageFileStream=isolatedStorageFile.CreateFile(fileName))
{
using(StreamWriterstreamWriter=newStreamWriter(isolatedStorageFileStream))
{
streamWriter.Write(content);
}
}
}
}
[/code]
读取一个文件:
privatestringReadFile(stringfileName)
[code]{
stringtext;
using(IsolatedStorageFileisolatedStorageFile=IsolatedStorageFile.GetUserStoreForApplication())
{
using(IsolatedStorageFileStreamisolatedStorageFileStream=isolatedStorageFile.OpenFile(fileName,FileMode.Open))
{
using(StreamReaderstreamReader=newStreamReader(isolatedStorageFileStream))
{
text=streamReader.ReadToEnd();
}
}
}
returntext;
}
[/code]
然后,可以用IsoStoreSpy这类辅助工具进应用程序查看存储的文件的内容等操作.
二、WindowsPhone8文件操作
wp8与win8的文件操作方式类似,如果你在win8下写过文件操作,那么WP8自然熟悉,这需要用到2个接口:IStorageFile和IStorageFolder,可以看出,一个是对文件的操作,一个是对目录的。这两个接口均在:Windwos.Storage组件中,
注意:因在windows8开发中大量使用了WinRT异步编程方式,故在WP8中编程亦如此。
写入文件:
publicasyncTaskWriteFile(stringfileName,stringtext)
[code]{
IStorageFolderapplicationFolder=ApplicationData.Current.LocalFolder;
IStorageFilestorageFile=awaitapplicationFolder.CreateFileAsync(fileName,CreationCollisionOption.ReplaceExisting);
using(Streamstream=awaitstorageFile.OpenStreamForWriteAsync())
{
byte[]content=Encoding.UTF8.GetBytes(text);
awaitstream.WriteAsync(content,0,content.Length);
}
}
[/code]
读取文件:
publicasyncTask<string>ReadFile(stringfileName)
[code]{
stringtext;
IStorageFolderapplicationFolder=ApplicationData.Current.LocalFolder;
IStorageFilestorageFile=awaitapplicationFolder.GetFileAsync(fileName);
IRandomAccessStreamaccessStream=awaitstorageFile.OpenReadAsync();
using(Streamstream=accessStream.AsStreamForRead((int)accessStream.Size))
{
byte[]content=newbyte[stream.Length];
awaitstream.ReadAsync(content,0,(int)stream.Length);
text=Encoding.UTF8.GetString(content,0,content.Length);
}
returntext;
}
[/code]
OK,到此我们将操作文件的方法写好后,就可以在业务层进行调用了,方式如下:
awaitWriteFile("TestWP8IO.txt","测试WP8与WP7文件操作");
[code]stringtext=awaitReadFile("TestWP8IO.txt")
[/code]
三、兼容性问题
上面的代码分别对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隔离存储目录.即:
IStorageFolderapplicationFolder=awaitApplicationData.Current.LocalFolder.GetFolderAsync("IsolatedStore");
最后,想说明的是:
如果你想“偷懒”或者说减少开发工作量,就看看我上面说的这些,但我建议各位开发者,最好是全新开发基于WP8平台的应用,毕竟这意味着你也可以在Win8中快速部署。
原文地址:http://jasonwei.com/archives/463
再续:
打开文件(需要你的手机上面装有这样的应用程序,例如PDF|OneNote)
IAsyncOperation<StorageFile>getFile=ApplicationData.Current.LocalFolder.GetFileAsync(filePath);
getFile.Completed=(IAsyncOperation<StorageFile>asyncInfo,AsyncStatusasyncStatus)=>
{
if(asyncStatus==AsyncStatus.Completed)
{
IStorageFilefile=getFile.GetResults();
IAsyncOperation<bool>isLaunch=Windows.System.Launcher.LaunchFileAsync(file);
isLaunch.Completed=(IAsyncOperation<bool>asyncInfo_launch,AsyncStatusasyncStatus_launch)=>
{
if(asyncStatus_launch==AsyncStatus.Completed)
{
Debug.WriteLine("isLaunch:"+isLaunch);
}
};
}
};
Launcher.LaunchFileAsync(IStorageFile)|launchFileAsync(IStorageFile)Method(Windows)
启动与指定的文件相关联的默认应用程序。publicstaticIAsyncOperation<bool>LaunchFileAsync(IStorageFilefile)
参数
file类型:
文件。
返回值
类型:生成操作。
备注
当调用API时,调用应用程序必须对用户可见。此API也对它可生成的文件类型有若干限制。很多包含可执行代码的文件类型,例如.exe、.msi和.js文件,在启动时都被阻止。恶意文件会修改系统,此限制可保护用户免受可能存在的恶意文件的危害。
当对上述任何原因生成失败时,API成功并从其异步操作的FALSE返回。因为其无法查询上述限制是否应用于当前生成,调用应用程序不应假定已成功生成,而应提供失败时的回退机制。可能的解决方案将是要求用户保存文件并指导用户在桌面打开它。
若要使用户选择应用程序而不是生成默认应用程序,请设置
若要显示文件可能不安全的警告,请设置
文件被传递给关联的应用程序。如果关联的应用程序为桌面应用程序,则使用外壳执行机制传递文件。
示例
此示例使用LaunchFileAsync(IStorageFile)|launchFileAsync(IStorageFile)生成包含在应用程序包中的文件。asyncvoidDefaultLaunch()
{
//Pathtothefileintheapppackagetolaunch
stringimageFile=@"images\test.png";
varfile=waitWindows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if(file!=null)
{
//Launchtheretrievedfile
varsuccess=awaitWindows.System.Launcher.LaunchFileAsync(file);
if(success)
{
//Filelaunched
}
else
{
//Filelaunchfailed
}
}
else
{
//Couldnotfindfile
}
}
相关文章推荐
- WP8开发系列3:WP7与WP8在文件IO操作上的区别
- WP8开发系列3:WP7与WP8在文件IO操作上的区别 推荐
- Android开发学习之IO文件读写操作
- ios开发系列之文件的写入读取,NSUserDefaults存储数据,NSFileManager操作文件和文件夹
- ios助手开发系列(五):文件操作系列2 - 操作文件和文件夹所需要开启的服务:AFC服务和AFC2服务
- .Net那点事儿系列:System.IO之windows文件操作
- IO操作系列之文件类型获取
- .Net那点事儿系列:System.IO之windows文件操作
- ios助手开发系列(四):文件操作系列1 - 获取设备文件信息 AFCFileInfoOpen函数和AFCKeyValueRead函数
- linux下C++开发系列(六)——文件IO相关的系统调用
- 浅谈无缓存IO操作和标准IO文件操作区别(转载+整理)
- Windows phone开发之文件夹与文件操作系列(一)文件夹与文件操作
- ios助手开发系列(六):文件操作系列3 - 文件上传和文件下载
- IO与文件的一些操作
- UWP开发技巧:实现SMB协议操作文件服务器文件
- 文件io操作
- Windows Phone 7(WP7)开发 网络操作(1) HttpWebRequest基本的GET请求
- android开发学习_文件操作
- 标准IO和文件IO的区别
- 标准IO与文件IO 的区别