silverlight.2.0:Silverlight(16) - 2.0数据的独立存储(Isolated Storage)

  本文源代码下载地址:

  http://flashview.ddvip.com/2008_12/Silverlight.rar

  介绍

  Silverlight 2.0 数据独立存储(Isolated Storage):

  IsolatedStorageFile - 操作 独立存储

  IsolatedStorageFile.GetUserStoreForSite - 按站点获取用户独立存储

  IsolatedStorageFile.GetUserStoreForApplication - 按应用获取用户独立存储

  IsolatedStorageSettings - 在独立存储中保存 key-value 字典表

  IsolatedStorageSettings.SiteSettings - 按站点保存 key-value 字典表

  IsolatedStorageSettings.ApplicationSettings - 按应用保存 key-value 字典表

  在线DEMO

  http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html

  举例

  IsolatedStorage.xaml

<UserControl x:Class="Silverlight20.Data.IsolatedStorage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel HorizontalAlignment="Left">
    <TextBox x:Name="txtMsg" Margin="5" />
    <TextBox x:Name="txtMsg2" Margin="5" />
    <Button x:Name="increase" Content="增加配额" Click="increase_Click" Margin="5" />
  </StackPanel>
</UserControl>


  IsolatedStorage.xaml.cs

using ;
using .Collections.Generic;
using .Linq;
using .Net;
using .Windows;
using .Windows.Controls;
using .Windows.Documents;
using .Windows.Input;
using .Windows.Media;
using .Windows.Media.Animation;
using .Windows.Shapes;
  
using .IO.IsolatedStorage;
using .IO;
  
Silverlight20.Data
{
  public partial IsolatedStorage : UserControl
  {
    public IsolatedStorage
    {
      InitializeComponent;
  
      // 演示 IsolatedStorageFile
      Demo;
  
      // 演示 IsolatedStorageSettings
      Demo2;
    }
  
    /**//// <summary>
    /// 演示 IsolatedStorageFile
    /// </summary>
    void Demo
    {
      // Isolated Storage - 独立存储个虚拟文件系统
  
      // IsolatedStorageFile - 操作 独立存储
      //   IsolatedStorageFile.GetUserStoreForSite - 按站点获取用户独立存储
      //   IsolatedStorageFile.GetUserStoreForApplication - 按应用获取用户独立存储
      
      // using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite)
      using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication)
      {
        // DirectoryExists(path) - 指定路径是否存在
        // CreateDirectory(path) - 创建指定路径
        // FileExists(path) - 指定文件是否存在
        // CreateFile(path) - 创建指定文件
        // GetDirectoryNames - 获取根目录下目录名
        // GetFileNames - 获取根目录下文件名
        // GetDirectoryNames(path) - 获取指定目录下目录名
        // GetFileNames(path) - 获取指定目录下文件名
        // OpenFile - 打开指定文件具体参数参看文档
        // DeleteFile(path) - 删除指定文件
        // DeleteDirectory(path) - 删除指定目录(要求目录存在且目录内无内容)
        // Remove - 关闭 IsolatedStorageFile 对象并移除独立存储内全部内容
  
        // 在根目录下创建指定目录
         (!isf.DirectoryExists("Directory01"))
          isf.CreateDirectory("Directory01");
         (!isf.DirectoryExists("Directory02"))
          isf.CreateDirectory("Directory02");
  
        // 创建指定子目录
         subDirectory01 = .IO.Path.Combine("Directory01", "SubDirectory01");
         subDirectory02 = .IO.Path.Combine("Directory01", "SubDirectory02");
         (!isf.DirectoryExists(subDirectory01))
          isf.CreateDirectory(subDirectory01);
         (!isf.DirectoryExists(subDirectory02))
          isf.CreateDirectory(subDirectory02);
  
        // 根目录下创建指定文件
         (!isf.FileExists("RootFile.txt"))
        {
          IsolatedStorageFileStream isfs = isf.CreateFile("RootFile01.txt");
          isfs.Close;
        }
  
        // 在指定目录下创建指定文件
         file01 = .IO.Path.Combine(subDirectory01, "File01.txt");
         file02 = .IO.Path.Combine(subDirectory01, "File02.txt");
         file03 = .IO.Path.Combine(subDirectory01, "File03.xml");
         (!isf.FileExists(file01))
        {
          // IsolatedStorageFileStream - 独立存储内文件流继承自 FileStream
          IsolatedStorageFileStream isfs = isf.CreateFile(file01);
          isfs.Close;
        }
         (!isf.FileExists(file02))
        {
          IsolatedStorageFileStream isfs = isf.CreateFile(file02);
          isfs.Close;
        }
         (!isf.FileExists(file03))
        {
          IsolatedStorageFileStream isfs = isf.CreateFile(file03);
          isfs.Close;
        }
  
        txtMsg.Text "根目录下目录列表:rn";
        // 获取根目录下目录名
        foreach ( directoryName in isf.GetDirectoryNames)
        {
          txtMsg.Text directoryName + "rn";
        }
  
        txtMsg.Text "根目录下文件列表:rn";
        // 获取根目录下文件名
        foreach ( fileName in isf.GetFileNames)
        {
          txtMsg.Text fileName + "rn";
        }
  
        txtMsg.Text "目录 Directory01 下目录列表:rn";
        // 获取 Directory01 目录下目录名
        foreach ( directoryName in isf.GetDirectoryNames(subDirectory01))
        {
          txtMsg.Text directoryName + "rn";
        }
  
        txtMsg.Text "目录 Directory01/SubDirectory01 下*.txt文件列表:rn";
        // 获取 Directory01/SubDirectory01 目录下后缀名为 txt 文件名
        foreach ( fileName in isf.GetFileNames(.IO.Path.Combine(subDirectory01, "*.txt")))
        {
          txtMsg.Text fileName + "rn";
        }
  
         (isf.FileExists(file01))
        {
          // 在文件 file01 中写入内容
          IsolatedStorageFileStream streamWrite = isf.OpenFile(file01, FileMode.Open, FileAccess.Write);
          using (StreamWriter sw = StreamWriter(streamWrite))
          {
            sw.WriteLine("我是:webabcd");
            sw.WriteLine("我专注于asp.net, Silverlight");
          }
  
          txtMsg.Text "文件 File01.txt 内容:rn";
          // 读取文件 file01 中内容
          IsolatedStorageFileStream streamRead = isf.OpenFile(file01, FileMode.Open, FileAccess.Read);
          using (StreamReader sr = StreamReader(streamRead))
          {
            txtMsg.Text sr.ReadToEnd;
          }
        }
  
        // 删除文件 file01
         (isf.FileExists(file01))
        {
          isf.DeleteFile(file01);
        }
  
        try
        {
          // 删除目录 subDirectory01
          isf.DeleteDirectory(subDirectory01);
        }
        catch (IsolatedStorageException ex)
        {
          // IsolatedStorageException - 操作临时存储失败时抛出异常
  
          // subDirectory01 目录内还有文件所以会抛异常
          txtMsg.Text ex.Message;
        }
      }
    }
  
    /**//// <summary>
    /// 演示 IsolatedStorageSettings
    /// </summary>
    void Demo2
    {
      // IsolatedStorageSettings - 在独立存储中保存 key-value 字典表
      //   IsolatedStorageSettings.SiteSettings - 按站点保存 key-value 字典表
      //   IsolatedStorageSettings.ApplicationSettings - 按应用保存 key-value 字典表
  
      IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
  
      // Add(key, value) - 添加对 key-value
      iss.Add("key", "value");
      txtMsg2.Text ()iss["key"] + "rn";
  
      // 修改指定 key value
      iss["key"] = "value2";
      txtMsg2.Text ()iss["key"] + "rn";
  
      // Remove(key) - 移除指定 key
      // Count - 字典表内 key-value 数
      iss.Remove("key");
      txtMsg2.Text iss.Count;
    }
   
    private void increase_Click(object sender, RoutedEventArgs e)
    {
      // 演示独立存储配额相关操作
      using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication)
      {
        // Quota - 当前配额(KB)
        // IncreaseQuotaTo(QuotaSize) - 增加到指定配额
        // AvailableFreeSpace - 当前可用配额
  
        isf.IncreaseQuotaTo(isf.Quota + 1 * 1024 * 1024);
  
        .Windows.Browser.HtmlPage.Window.Alert(
          .Format("当前配额:{0};可用配额:{1}", isf.Quota, isf.AvailableFreeSpace));
      }
    }
  }
}




  演示 IsolatedStorageFile 运行结果:

  根目录下目录列表:

  Directory01

  Directory02

  根目录下文件列表:

  RootFile01.txt

  __LocalSettings

  目录 Directory01 下目录列表:

  SubDirectory01

  目录 Directory01/SubDirectory01 下*.txt文件列表:

  File01.txt

  File02.txt

  文件 File01.txt 内容:

  我是:webabcd

  我专注于asp.net, Silverlight

  无法删除目录不为空或不存在

  演示 IsolatedStorageSettings 运行结果:

  value

  value2

  0

  OK



Tags:  silverlight2 silverlight是什么 silverlight silverlight.2.0

延伸阅读

最新评论

发表评论