Welcome

首页 / 软件开发 / Silverlight / 一起学Windows Phone 7开发(六.Isolate Storage)

一起学Windows Phone 7开发(六.Isolate Storage)2010-11-07 博客园 小镇windows phone 7 目前版本上已确定没有文件系统(也就是说filestream、OpenFileDialog这样的功能都是不能使用了)和数据库系统了,那在开发时需要保存一些用户配置信息或临时数据在本地怎么办? 答案是只能使用的特色功能Isolate Storage来保存文本文件、XML文件或INI文件的方式来替代了。

其实使用Isolate Storage的最大好处就是安全性了,因为只有本程序可以访问该区域,而其他程序是无法访问的。这样也就可以对一此敏感数据的保存不用自已再加密了。但是这个区域是有限的(默认为2GB),不能够保存很大的数据,以及长期保存数据。如果您非要保存大量数据,以及长期保存的话,目前只能保存在云端而不是本地。

对于Isolate Storage的使用,其实也很简单,和FileStream基本上是一样的。下面就是一些基本操作的代码:

1.打开Isolate Storage:

首先引入命名空间System.IO.IsolatedStorage;

打开storage IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

2.创建文件夹:

isf.CreateDirectory("MyFolder");

3.创建文件:

StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream("MyFolder\myFile.txt", FileMode.OpenOrCreate, isf));
writer.WriteLine(“Hello World!”);
writer.Close();

4.读取文件:

StreamReader reader = new StreamReader(new IsolatedStorageFileStream("MyFolder\myFile.txt", FileMode.Open, isf));
string text = reader.ReadLine();

5.删除文件:

isf.DeleteFile("MyFolder\myFile.txt");

6.删除文件夹:

isf.DeleteDirectory("MyFolder");

7.判断文件或文件夹是否存在:

isf.FileExit("MyFolder\myFile.txt");

isf.DirectoryExit("MyFolder");

8.也可以使用IsolateStorageFileStream来操作文件或文件夹,用法和FileStream 基本相同.

相关的API详细信息可以查看

http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage(VS.95).aspx