Silverlight - 隔离存储

第三种文件访问机制是 Isolated Storage 机制,它提供与登录用户关联的存储. API通过 .NET System.IO 命名空间中的 Stream 类提供数据.因此,与我们到目前为止所看到的其他机制一样,您可以使用 System.IO 中的其他类型来处理流,使您能够存储文本或二进制数据.

一些重要的功能是 :

  • 此存储机制称为隔离存储因为商店是分区的,而Silverlight应用程序只能访问某些部分.

  • 您无法访问任何旧的存储数据.首先,商店按用户分区. Silverlight应用程序无法访问与登录用户不同的用户以及运行应用程序的商店.

  • 这与任何标识无关您的Web应用程序可能使用的机制这是一个值得记住的重点,因为一些共享计算机的人不会打扰单独的Windows帐户,并且习惯于登录和退出他们使用的网站.

使用隔离存储

隔离存储不是Silverlight独有的.该API最初是为 Windows Forms 引入的,用于启用从Web启动的应用程序,以便在部分信任方案中本地存储数据.实现方式不同,无法从Silverlight访问完整的 .NET 框架的隔离存储,反之亦然.

但是,如果您已经使用过它,这里的步骤看起来非常熟悉.

  • 首先要求用户特定的商店.在这种情况下,我们要求申请一个.如果我们想要网站上所有XAP共享的每站点商店,我们会调用 GetUserStoreForSite .

  • 这两种方法都返回一个 IsolatedStorageFile 对象,这是一个非常无用的名称,因为它代表一个目录,而不是一个文件.

  • 要访问文件,你需要向 IsolatedStorageFile 询问.

  • 我们使用 IsolatedStorageFileStream class,它的构造函数要求你传递 IsolatedStorageFile 对象作为参数.

  • 所以我们正在创建一个新的文件在商店里.磁盘上文件的确切位置是未知的.

  • 包含目录具有随机元素,以便无法猜出文件名.

  • 如果没有这个,恶意网站可能会在用户的计算机上放置一个文件,然后构建一个文件URL来打开它,希望愚弄用户点击在本地执行程序的链接.

  • Windows中内置了各种其他保护措施,试图防止这种情况发生,但这是另一层防御,以防其他人以某种方式被禁用或绕过.

  • 该文件将存储在用户个人资料中的某处,但这同样多你可以知道它.您的 IsolatedStorageFileStream 不会报告其真实位置.

让我们看一下跟踪的简单示例应用程序运行了多少次.下面给出的是XAML代码.

<UserControl x:Class = "StoreRunCount.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   mc:Ignorable = "d" 
   d:DesignHeight = "300" d:DesignWidth = "400"> 
   
   <Grid x:Name = "LayoutRoot" Background = "White"> 
      <TextBlock x:Name = "runCountText" FontSize = "20" /> 
   </Grid> 
	
</UserControl>

以下是使用隔离存储空间的C#代码.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

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

namespace StoreRunCount { 

   public partial class MainPage : UserControl {
	
      const string RunCountFileName = "RunCount.bin"; 
		
      public MainPage() { 
         InitializeComponent();  
         int runCount = 0;  
			
         using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { 
			
            if (store.FileExists(RunCountFileName)) { 
               using (var stm = store.OpenFile(RunCountFileName, 
                  FileMode.Open, FileAccess.Read)) 
               using (var r = new BinaryReader(stm)) { 
                  runCount = r.ReadInt32(); 
               }  
            } 
			
            runCount += 1;  
            using (var stm = store.OpenFile(RunCountFileName, 
               FileMode.Create, FileAccess.Write)) 
					
            using (var w = new BinaryWriter(stm)) { 
               w.Write(runCount); 
            } 
         }  
			
         runCountText.Text = "You have run this application " + runCount.ToString() + " time(s)"; 
      } 
   }
}

编译并执行上述代码后,您将看到以下网页向您显示运行此应用程序的次数.

隔离存储

增加配额

如果由于某种原因初始数量不足,应用程序可能会要求更多空间.无法保证请求会成功. Silverlight会询问用户是否愿意为应用程序提供更多空间.

顺便提一下,您只能在响应用户输入时要求更多存储空间,例如点击的.如果您尝试在其他时间(例如插件加载时)或计时器处理程序中询问它,Silverlight将自动使请求失败,甚至不会提示用户.额外配额仅适用于与用户交互的应用程序.

IsolatedStorageFile 对象提供三个成员来管理配额和减号;

  • AvailableFreeSpace

  • IncreaseQuotaTo

  • 配额

AvailableFreeSpace

AvailableFreeSpace属性告诉您剩余的配额有多少.

请注意,即使空子目录会占用一些配额,因为操作系统需要在磁盘上分配空间来表示目录.因此,可用空间可能小于总配额,减去所有文件的总和大小.

IncreaseQuotaTo

如果你没有有足够的空间继续进行,你可以通过调用 IncreaseQuotaTo 方法来要求更多.

配额

这里我们使用的是第三个属性,配额,以发现当前的配额大小,然后我们添加额外的金额以获取新的请求配额.

该方法返回 True False ,以表明我们是否分配了我们要求的内容.请注意,Silverlight可能会决定分配比您要求的更多的空间.

这是一个简单的示例,用于在单击按钮时增加配额.下面给出的是XAML代码.

<UserControl x:Class = "ChangeQuota.MainPage" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   mc:Ignorable = "d" 
   d:DesignHeight = "300" d:DesignWidth = "400"> 
   
   <Grid x:Name = "LayoutRoot" Background = "White"> 
      <TextBlock x:Name = "infoText" FontSize = "20" TextWrapping = "Wrap" />  
      <Button x:Name = "increaseQuota" Content = "Increase" HorizontalAlignment = "Center" 
         FontSize = "20" 
         VerticalAlignment = "Center" Click = "increaseQuota_Click" /> 
   </Grid>
	
</UserControl>

以下是点击事件的实施,其中增加了配额.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input;
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

using System.IO.IsolatedStorage;
  
namespace ChangeQuota { 

   public partial class MainPage : UserControl { 
	
      public MainPage() { 
         InitializeComponent(); 
      } 
	  
      private void increaseQuota_Click(object sender, RoutedEventArgs e) { 
         using (IsolatedStorageFile isoStore = 
            IsolatedStorageFile.GetUserStoreForApplication()) { 
               long newQuota = isoStore.Quota + 10240; 
					
               if (isoStore.IncreaseQuotaTo(newQuota)) { 
                  infoText.Text = "Quota is " + isoStore.Quota + ", free space: " + 
                  isoStore.AvailableFreeSpace; 
               } else { 
                  infoText.Text = "Meanie!"; 
               } 
         } 
      } 
   } 
}

编译上面的代码时执行后,您将看到以下输出.

隔离存储配额

单击增加时,会出现提示.它要求将配额增加到比现有规模大10KB.

隔离增加配额

当您单击时,它会打印出可用配额数量.

增加配额

我们建议您执行上述示例以便更好地理解.