详解Silverlight 2中的独立存储(Isolated Storage)

概述

独立存储(Isolated Storage)是Silverlight 2中提供的一个客户端安全的存储,它是一个与Cookie机制相似的局部信任机制。独立存储机制的APIs 提供了一个虚拟的文件系统和能够访问这个虚拟文件系统的数据流对象。Silverlight中的独立存储是基于 .NET Framework中的独立存储来创建的,因此它仅仅是.NET Framework中独立存储的一个子集。
Silverlight中的独立存储有如下一些特征:
1.每一个基于Silverlight的应用程序都被分配了属于它本身的一部分存储空间, 可是应用程序中的程序集倒是在存储空间中共享的。一个应用程序被服务器赋给了一个惟一的固定的标识值。基于Silverlight的应用程序的虚拟文件系统如今就以一个标识值的方式来访问了。这个标识值必须是一个常量,这样每次应用程序运行时才能够找到这个共享的位置。  
2.独立存储的APIs 其实和其它的文件操做APIs相似,好比 File 和 Directory 这些用来访问和维护文件或文件夹的类。 它们都是基于FileStream APIs 来维护文件的内容的。
3.独立存储严格的限制了应用程序能够存储的数据的大小,目前的上限是每一个应用程序为1 MB。

使用独立存储

Silverlight中的独立存储功能经过密封类IsolatedStorageFile来提供,位于命名空间System.IO.IsolatedStorag中,IsolatedStorageFile类抽象了独立存储的虚拟文件系统。建立一个 IsolatedStorageFile 类的实例,可使用它对文件或文件夹进行列举或管理。一样还可使用该类的 IsolatedStorageFileStream 对象来管理文件内容,它的定义大概以下所示:
TerryLee_0072
在Silverlight 2中支持两种方式的独立存储,即按应用程序存储或者按站点存储,能够分别使用GetUserStoreForApplication方法和GetUserStoreForSite方法来获取IsolatedStorageFile对象。下面看一个简单的示例,最终的效果以下图所示:
  TerryLee_0073
下面来看各个功能的实现:
建立目录,直接使用CreateDirectory方法就能够了,另外还可使用DirectoryExistes方法来判断目录是否已经存在:
void btnCreateDirectory_Click(object sender, RoutedEventArgs e)
{
    using (IsolatedStorageFile store =
                    IsolatedStorageFile.GetUserStoreForApplication())
    {
        String directoryName = this.txtDirectoryName.Text;
        if (this.lstDirectories.SelectedItem != null)
        {
            directoryName = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(),
                            directoryName);
        }
        if (!store.DirectoryExists(directoryName))
        {
            store.CreateDirectory(directoryName);
            HtmlPage.Window.Alert("建立目录成功!");
        }
    }
}
建立文件,经过CreateFile方法来获取一个IsolatedStorageFileStream,并将内容写入到文件中:
void btnCreateFile_Click(object sender, RoutedEventArgs e)
{
    if (this.lstDirectories.SelectedItem == null &&
        this.txtDirectoryName.Text == "")
    {
        HtmlPage.Window.Alert("请先选择一个目录或者输入目录名");
        return;
    }
    using (IsolatedStorageFile store =
                       IsolatedStorageFile.GetUserStoreForApplication())
    {
        String filePath;
        if (this.lstDirectories.SelectedItem == null)
        {
            filePath = System.IO.Path.Combine(this.txtDirectoryName.Text,
                            this.txtFileName.Text + ".txt");
        }
        else
        {
            filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(),
                            this.txtFileName.Text + ".txt");
        }
        
        IsolatedStorageFileStream fileStream = store.CreateFile(filePath);
        using (StreamWriter sw = new StreamWriter(fileStream))
        {
            sw.WriteLine(this.txtFileContent.Text);
        }
        fileStream.Close();
        HtmlPage.Window.Alert("写入文件成功!");
    }
}
读取文件,直接使用System.IO命名空间下的StreamReader:
void btnReadFile_Click(object sender, RoutedEventArgs e)
{
    if (this.lstDirectories.SelectedItem == null ||
        this.lstFiles.SelectedItem == null)
    {
        HtmlPage.Window.Alert("请先选择目录和文件!");
        return;
    }
    using (IsolatedStorageFile store =
                       IsolatedStorageFile.GetUserStoreForApplication())
    {
        String filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(),
                          this.lstFiles.SelectedItem.ToString());
        if (store.FileExists(filePath))
        {
            StreamReader reader = new StreamReader(store.OpenFile(filePath,
                   FileMode.Open, FileAccess.Read));
            this.txtFileContent.Text = reader.ReadToEnd();
            this.txtDirectoryName.Text = this.lstDirectories.SelectedItem.ToString();
            this.txtFileName.Text = this.lstFiles.SelectedItem.ToString();
        }
    }
}
删除目录和文件:
void btnDeleteFile_Click(object sender, RoutedEventArgs e)
{
    if (this.lstDirectories.SelectedItem != null &&
       this.lstFiles.SelectedItem != null)
    {
        using (IsolatedStorageFile store =
                       IsolatedStorageFile.GetUserStoreForApplication())
        {
            String filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(),
                    this.lstFiles.SelectedItem.ToString());
            store.DeleteFile(filePath);
            HtmlPage.Window.Alert("删除文件成功!");
        }
    }
}
void btnDeleteDirectory_Click(object sender, RoutedEventArgs e)
{
    if (this.lstDirectories.SelectedItem != null)
    {
        using (IsolatedStorageFile store =
                       IsolatedStorageFile.GetUserStoreForApplication())
        {
            store.DeleteDirectory(this.lstDirectories.SelectedItem.ToString());
            HtmlPage.Window.Alert("删除目录成功!");
        }
    }
}
获取目录列表和文件列表:
void lstDirectories_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (lstDirectories.SelectedItem != null)
    {
        using (IsolatedStorageFile store =
                        IsolatedStorageFile.GetUserStoreForApplication())
        {
            String[] files = store.GetFileNames(
                this.lstDirectories.SelectedItem.ToString() + "/");
            this.lstFiles.ItemsSource = files;
        }
    }
}
void BindDirectories()
{
    using (IsolatedStorageFile store = 
                    IsolatedStorageFile.GetUserStoreForApplication())
    {
        String[] directories = store.GetDirectoryNames("*");
        this.lstDirectories.ItemsSource = directories;
    }
}

增长配额

在本文一开始我就提到独立存储严格的限制了应用程序能够存储的数据的大小,可是咱们能够经过IsolatedStorageFile类提供的IncreaseQuotaTo方法来申请更大的存储空间,空间的大小是用字节做为单位来表示的,以下代码片断所示,申请独立存储空间增长到5M:
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
    long newQuetaSize = 5242880;
    long curAvail = store.AvailableFreeSpace;
    if (curAvail < newQuetaSize)
    {
        store.IncreaseQuotaTo(newQuetaSize);
    }
}
当咱们试图增长空间大小时浏览器将会弹出一个确认对话框,供咱们确认是否容许增长独立存储的空间大小。
TerryLee_0076 

文件被存往何处

既然独立独立存储是存放在客户端本地,那到底存放在何处呢?在我我的计算机上的地址为:C:\Users\TerryLee\AppData\LocalLow\Microsoft\Silverlight\is\035kq51b.2q4\pksdhgue.3rx\1,不一样机器会有一些变化,另外在XP下的存储位置与Vista是不相同的。在g文件夹下面,咱们找到当前应用程序的一些公有信息,能够看到有以下三个文件:
TerryLee_0074
id.dat记录了当前应用程序的ID
quota.dat记录了当前应用程序独立存储的配额,即存储空间大小
used.dat记录已经使用的空间
在另外一个s文件夹下能够找到咱们建立的目录以及文件,而且能够打开文件来看到存储的内容,以下图所示:
TerryLee_0075

禁用独立存储

如今咱们来思考一个问题,既然独立存储是一个与Cookie机制相似的局部信任机制,咱们是否也能够禁用独立存储呢?答案天然是确定的。在Silverlight应用程序上点击右键时,选择Silverlight Configuration菜单,将会看到以下窗口:
TerryLee_0077
在这里咱们能够看到每个应用程序存储空间的大小以及当前使用的空间;能够删除应用程序独立存储数据或者禁用独立存储的功能。

独立存储配置

最后在简单说一下独立存储配置,在Beta 1时代是应用程序配置,如今不只支持应用程序配置,同时还支持站点配置,咱们能够用它来存储应用程序配置如每一个页面显示的图片数量,页面布局自定义配置等等,使用IsolatedStorageSettings类来实现,该类在设计时使用了字典来存储名-值对,它的使用至关简单:
IsolatedStorageSettings appSettings = 
    IsolatedStorageSettings.ApplicationSettings;
appSettings.Add("mykey","myValue");
appSettings.Save();
IsolatedStorageSettings siteSettings =
    IsolatedStorageSettings.SiteSettings;
siteSettings.Add("mykey1","myValue1");
siteSettings.Save();
独立存储配置的机制与咱们上面讲的同样,它也是基于本地文件存储,系统默认的会建立一个名为__LocalSettings的文件进行存储,以下图所示:
TerryLee_0078
打开文件后能够看到,存储的内容(此处进行了整理)
<ArrayOfKeyValueOfstringanyType 
  xmlns:i="[url]http://www.w3.org/2001/XMLSchema-instance[/url]"
  xmlns="[url]http://schemas.microsoft.com/2003/10/Serialization/Arrays[/url]">
  <KeyValueOfstringanyType>
    <Key>mykey</Key>
    <Value xmlns:d3p1="[url]http://www.w3.org/2001/XMLSchema[/url]" 
           i:type="d3p1:string">myValue</Value>
  </KeyValueOfstringanyType>
</ArrayOfKeyValueOfstringanyType>
值得一提的是使用独立存储配置不只仅能够存储简单类型的数据,也能够存储咱们自定义类型的数据。

小结

本文详细介绍了Silverlight 2中的独立存储机制,但愿对你们有所帮助。
示例下载:

0javascript

收藏java

lihuijun

203篇文章,79W+人气,0粉丝

Ctrl+Enter 发布linux

发布git

取消windows

3条评论浏览器

按时间倒序 按时间正序

0安全

3
分享
lihuijun

推荐专栏更多

带你玩转高可用

前百度高级工程师的架构高可用实战

共15章 | 曹林华

¥51.00 524人订阅
负载均衡高手炼成记

高并发架构之路

共15章 | sery

¥51.00 607人订阅
基于Python的DevOps实战

自动化运维开发新概念

共20章 | 抚琴煮酒

¥51.00 575人订阅
网工2.0晋级攻略 ——零基础入门Python/Ansible

网络工程师2.0进阶指南

共30章 | 姜汁啤酒

¥51.00 2076人订阅
全局视角看大型园区网

路由交换+安全+无线+优化+运维

共40章 | 51CTOsummer

¥51.00 2641人订阅

扫一扫,领取大礼包

相关文章
相关标签/搜索