昊漫玉 发表于 2015-5-13 05:42:05

Windows phone 7 之Isolated Storage

Silverlight的IsolatedStorage是一种类似Cookie的静态存储机制.可以将一些基本类型(String,Int)的信息甚至是自定义类型序列化后的静态存储于客户端文件中.
独立存储是一个局部信任机制.当你创建一个Silverlight应用程序时会在硬盘上创建相应独立的存储区域.
这里面独立是相对于不同Silverlight Project而言的. 当然如果应用程序中存在多个程序集,那么存储空间在这多个程序集之间是共享的.
Silverlight限制了客户端Silverlight应用程序不能访问全部的文件系统,只能通过独立存储机制提供虚拟文件系统,访问数据流对象. 这样一来类似我们Application 有了自己一块硬盘空间一样.独立存储空间内就可以放置任意类型的文件. XML /.txt等.


命名空间为:using System.IO.IsolatedStorage;

      IsolatedStorageFile Storge;
      private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
      {//打开存储
            Storge = IsolatedStorageFile.GetUserStoreForApplication();
      }
      private void button1_Click(object sender, RoutedEventArgs e)
      {//建立文件夹
            Storge.CreateDirectory("我的文件夹");
      }
      private void button2_Click(object sender, RoutedEventArgs e)
      {//建立文件
            StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream("我的文件夹\\File.txt", FileMode.OpenOrCreate, Storge));
            writer.WriteLine("Text里面的内容");
            writer.Close();
      }
      private void button3_Click(object sender, RoutedEventArgs e)
      {//读取文件
            StreamReader reader = new StreamReader(new IsolatedStorageFileStream("我的文件夹\\File.txt", FileMode.Open, Storge));
            string text = reader.ReadLine();
            MessageBox.Show(text);
      }
      private void button4_Click(object sender, RoutedEventArgs e)
      {//删除文件
            Storge.DeleteFile("我的文件夹\\File.txt");
      }
      private void button5_Click(object sender, RoutedEventArgs e)
      {//删除文件夹
            Storge.DeleteDirectory("我的文件夹");
      }
      private void button6_Click(object sender, RoutedEventArgs e)
      {
            //判断文件是否存在
            Storge.FileExists("我的文件夹\\File.txt");
            //判断文件夹是否存在
            Storge.DirectoryExists("我的文件夹");
      }


关于IsolatedStorage的七个Best Practice:


[*]Wrap all calls to isolated storage within try/catch blocks to be resilient to potential IsolatedStorageExceptions, which can be thrown if isolated storage is disabled or if the store has been deleted.
  把所有对Isolated Storage的调用都包裹在Try/Catch块中来捕获潜在的IsolatedStorageExceptions。因为导致异常的因素实在很多(禁用独立存储、独立存储空间不足、多个应用程序同时操作一份独立存储导致的不一致性,还有在某些情况下操作独立存储,可能会提示独立存储不可用)。


[*]If your Silverlight application needs to store a lot of data in isolated storage, consider hosting it on its own site so that it won't affect other applications on the site and other applications won't affect it.
  如果你的Silverlight应用程序需要独立存储很多的数据,最好把它放在一个单独的网站,这样就不会影响到其他的应用程序。因为所有放在同一网站下的Silverlight应用程序,使用的是同一定额,它们会互相挤占。


[*]If you have a group of Silverlight applications that need to share data on the client, host them on the same site.
  如果几个Silverlight应用程序需要在客户共享数据,把它们放在同一网站下。


[*]Keep isolated storage paths as small as possible to prevent the internal full path from reaching the 260-character limit.
  让独立存储的路径越小越好,避免路径长度达到260个字符的限制。


[*]Encrypt sensitive data stored in isolated storage.
  加密保存在Isolated Storage里的敏感数据,因为它们实际是文本文件,直接可读的。


[*]Use IsolatedStorageSettings to store objects and simple settings in isolated storage.
  使用IsolatedStorageSettings来储存对象和简单的设置。


[*]Use IsolatedStorageFile if you want to use file and stream-based APIs, are storing large amounts of data, or need fine-grained control over the contents of isolated storage.
  如果需要使用文件或者基于流的API,保存大数据,或者需要细粒度的控制独立存储中的内容,则使用IsolatedStorageFile。
页: [1]
查看完整版本: Windows phone 7 之Isolated Storage