WP7的独立存储

最近在做一个Windows phone 7上的客户端程序,较为简单,接触了WP7的一些东西,稍作笔记。WP7上的开发,是silverlght的又一次推广,所以入手容易,开发出来的效果也会很华丽。由Visual Studio、Expression Blend的强大支持,开发效率也会很高。本篇记叙下WP7的存储方式-独立存储。在参与的项目中,是用于保存用户添加的应用。

      WP7 没有本地数据库API可以利用,提供的有XML、独立存储和云存储。Isolated Storage[独立存储]有两种方式在本地存储你的数据。第一是通过库中的键/值对,叫做IsolatedStorageSettings。第二是通过创建真实的文件和目录,叫做IsolatedStorageFile

      IsolatedStorageSettings:

[c-sharp]  view plain copy
  1. /*使用 ApplicationSettings 属性可创建用于在独立存储中存储键/值对的字典的新实例. 
  2.  * ApplicationSettings 特定于某个用户和某个应用程序。 
  3.  */  
  4. IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;  
  5. //添加键值对  
  6. settings.Add("flag"false);  
  7. //读取键值对  
  8. bool flag = (bool)settings["flag"];  
  9. //设置键值对  
  10. settings["flag"] = true;  
  11. //判断某键是否存在  
  12. bool isExist = settings.Contains("flag");  
 

      IsolatedStorageFile:

[c-sharp]  view plain copy
  1.  using System.IO.IsolatedStorage;  
  2.  //为程序获取一个虚拟的本地存储  
  3.  IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();  
  4.  //创建一个新的文件夹  
  5.  fileStorage.CreateDirectory("textFiles");  
  6.  /* 建立文件流,参数分别为:文件路径、打开模式、存储区。 
  7.   * 打开模式有 
  8.   * FileMode.Append-Opens the file if it exists and seeks to the end of the file, or creates a new file. 
  9.   * FileMode.CreateNew-Specifies that the operating system should create a new file. 
  10.   * FileMode.Truncate-Specifies that the operating system should open an existing file.  
  11.   *      Once opened, the file should be truncated so that its size is zero bytes. 
  12.   * FileMode.Create-Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. 
  13.   *      System.IO.FileMode.Create is equivalent to requesting that if the file does not exist, use System.IO.FileMode.CreateNew;  
  14.   *      otherwise, use System.IO.FileMode.Truncate. 
  15.   * FileMode.Open-Specifies that the operating system should open an existing file. 
  16.   * FileMode.OpenOrCreate-Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. 
  17.   *  
  18.   */  
  19. IsolatedStorageFileStream isolatedFileStream =  
  20.     new IsolatedStorageFileStream("textFiles//newText.txt", FileMode.OpenOrCreate, fileStorage);  
  21.    
  22. //写入  
  23. StreamWriter fileWriter = new StreamWriter(isolatedFileStream);  
  24. //向文件中写出内容  
  25. fileWriter.WriteLine("write data");  
  26. //关闭StreamWriter.  
  27. fileWriter.Close();  
  28. //读取  
  29. StreamReader fileReader=new StreamReader(isolatedFileStream);  
  30. String str=fileReader.ReadLine();  
  31. fileReader.Close();  
  32. //判断存储区某文件是否存,参数是文件路径  
  33. fileStorage.FileExists("file path");  
 

补充下:WP7的开发环境,下载Microsoft Visual Studio 2010 Express For Windows Phone,安装成功后,就自带有模拟器了。操作系统应该是Windows 7,在XP下,模拟器启动有问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值