zoukankan      html  css  js  c++  java
  • WP7 开发(三) Isolate Storage(保存数据)

    windows phone 7 目前版本上已确定没有文件系统(也就是说filestreamOpenFileDialog这样的功能都是不能使用了)和数据库系统了,Windows Phone 7 支持访问数据几种方式为: XML、Isolated Storage[独立存储]、Cloud[云存储],Windows Phone 7 上没有本地数据库API可以利用 。

    Isolated Storage[独立存储]有两种方式在本地存储你的数据。第一是通过库中的键/值对,叫做IsolatedStorageSettings。第二是通过创建真实的文件和目录,叫做IsolatedStorageFile。

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

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

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

    (1)IsolatedStorageSettings

      IsolatedStorageSettings允许你在一个字典中存储键/值对(注意,无需任何设定),然后再读取出来。这些数据会一直保存着,无论应用程序停止/启动,或者关机等等。除非你删除它,或者用户卸载你的应用程序,否则它一直存在。要记住的一点是在它被添加到字典中之前你无法读取它。

      using System.IO.IsolatedStorage;

      IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
          //使用 ApplicationSettings 属性可创建用于在独立存储中存储键/值对的字典的新实例。
          //ApplicationSettings 特定于某个用户和某个应用程序。应用程序范围由应用程序的完整路径决定。
            private void InitializeSettings()
            {
                if (settings.Contains("emailFlag"))
                {
                    EmailFlag.IsChecked = (bool)settings["emailFlag"];
                }
                else settings.Add("emailFlag", false);
            }

            private void EmailFlag_Unchecked(object sender, RoutedEventArgs e)
            {
                settings["emailFlag"] = false;
            }

            private void EmailFlag_Checked(object sender, RoutedEventArgs e)
            {
                settings["emailFlag"] = true;
            }
       

    (2)IsolatedStorageFile

      IsolatedStorageFile表示包含文件和目录的独立存储区。使用IsolatedStorageFile是一种让你可以在用户的设备中存储真实文件的机制。

         该类使独立存储的虚拟文件系统抽象化。IsolatedStorageFile 对象对应于特定的独立存储范围,在该范围中存在由 IsolatedStorageFileStream 对象表示的文件。应用程序可以使用独立存储将数据保存在文件系统中这些数据自己的独立部分,而不必在文件系统中指定特定的路径。

         虚拟文件系统的根位于物理文件系统上经过模糊处理的每用户文件夹中。由主机提供的每个唯一标识符都映射为不同的根,该根为每个应用程序提供它自己的虚拟文件系统。应用程序不能从它自己的文件系统导航到另一个文件系统中。

         因为独立存储区在特定程序集的范围内,所以其他大多数托管代码都不能访问您的代码的数据(高度受信任的托管代码和管理工具可以从其他程序集访问存储区)。非托管代码可以访问任何独立存储区。 

    1. 打开Isolate Storage

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

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

      2. 创建文件夹:

              isf.CreateDirectory("MyFolder");

      3. 创建文件:

        StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\my          File.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 基本相同.

      例子:
      在一个子目录中创建了一个文本文件,并读取文件中的内容。我们还可以创建和删除目录,子目录及文件。创建一个新的

      IsolatedStorageFile对象,并使用一个IsolatedStorageFileStream对象将它写入到驱动器中。

      using System.IO.IsolatedStorage;
      using System.IO;

      private void SaveButton_Click(object sender, RoutedEventArgs e)
      {
          //为程序获取一个虚拟的本地存储
          IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();

          //创建一个新的文件夹
          fileStorage.CreateDirectory("textFiles");

          //创建一个txt文件的流
          StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("textFiles\\newText.txt",  

        FileMode.OpenOrCreate, fileStorage));
          //向文件中写出内容
          fileWriter.WriteLine(writeText.Text);
          //关闭StreamWriter.
          fileWriter.Close();
      }

      private void GetButton_Click(object sender, RoutedEventArgs e)
      {
          //为程序获取一个虚拟的本地存储
          IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
          //创建一个新的StreamReader
          StreamReader fileReader = null;

          try
          {
              //读取文件
              fileReader = new StreamReader(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.Open,

          fileStorage));
              //读取内容
              string textFile = fileReader.ReadLine();
              viewText.Text = textFile;
              fileReader.Close();
          }
          catch
          {
              viewText.Text = "Need to create directory and the file first.";
          }
      }

  • 相关阅读:
    [再寄小读者之数学篇](2015-06-24 积分不等式)
    揭秘:三国时能令诸葛亮自叹不如的奇才是谁?
    【大话三国】揭秘蜀汉五虎将的真相
    三国揭秘 诸葛亮为何重用张飞疏远关羽
    [再寄小读者之数学篇](2015-06-08 一个有意思的定积分计算)
    咏史---左思
    非洲雄狮捕猎未遂被野牛群追赶逃到树上
    诚信,聪明,快乐,地位与竞争
    [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.26
    [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.11
  • 原文地址:https://www.cnblogs.com/xingfuzzhd/p/2248178.html
Copyright © 2011-2022 走看看