Windows Phone开发中IsolateStorage用法指引 |
|
本文标签:WindowsPhone开发 本文和大家重点学习一下Windows Phone开发中IsolateStorage的用法,在开发时使用silverlight的特色功能IsolateStorage来保存文本文件、XML文件或INI文件的方式来替代了 。 Windows Phone开发中IsolateStorage windowsphone7目前版本上已确定没有文件系统(也就是说filestream、OpenFileDialog这样的功能都是不能使用了)和数据库系统了,那在开发时需要保存一些用户配置信息或临时数据在本地怎么办?答案是只能使用silverlight的特色功能IsolateStorage来保存文本文件、XML文件或INI文件的方式来替代了 。 其实使用IsolateStorage的最大好处就是安全性了,因为只有本程序可以访问该区域,而其他程序是无法访问的 。这样也就可以对一此敏感数据的保存不用自已再加密了 。但是这个区域是有限的(默认为2GB),不能够保存很大的数据,以及长期保存数据 。如果您非要保存大量数据,以及长期保存的话,目前只能保存在云端而不是本地 。 对于Windows Phone开发中IsolateStorage的使用,其实也很简单,和FileStream基本上是一样的 。下面就是一些基本操作的代码: 1.打开IsolateStorage: 首先引入命名空间System.IO.IsolatedStorage; 打开storageIsolatedStorageFileisf=IsolatedStorageFile.GetUserStoreForApplication(); 2.创建文件夹: isf.CreateDirectory("MyFolder"); 3.创建文件: StreamWriterwriter=newStreamWriter(newIsolatedStorageFileStream("MyFolder\\myFile.txt",FileMode.OpenOrCreate,isf)); writer.WriteLine(“HelloWorld!”); writer.Close(); 4.读取文件: StreamReaderreader=newStreamReader(newIsolatedStorageFileStream("MyFolder\\myFile.txt",FileMode.Open,isf)); stringtext=reader.ReadLine(); 5.删除文件: isf.DeleteFile("MyFolder\\myFile.txt"); 6.删除文件夹: isf.DeleteDirectory("MyFolder"); 7.判断文件或文件夹是否存在: isf.FileExit("MyFolder\\myFile.txt"); isf.DirectoryExit("MyFolder"); 8.也可以使用Windows Phone开发中IsolateStorageFileStream来操作文件或文件夹,用法和FileStream基本相同. 相关的API详细信息可以查看 http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage(VS.95).aspx
|