日期:2009-05-30  浏览次数:20420 次

获得存储区
存储区公开数据舱中的虚文件系统。IsolatedStorageFile 提供了许多与存储区进行交互的方法。要创建和检索存储区,IsolatedStorageFile 提供了三种静态方法。调用 GetUserStoreForAssembly 或 GetUserStoreForDomain 分别返回按用户和程序集隔离及按用户、域和程序集隔离的存储。这两种方法检索属于代码块(是从该代码块中调用这两种方法的)的存储区。静态方法 GetStore 返回独立存储区,该存储区是通过传入范围参数组合指定的。下面的参数返回一个按用户、程序集和域隔离的存储区。

[C#]
GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);
GetStore 方法可以用于指定存储区应该和漫游用户配置文件一起漫游。

默认情况下,从不同的程序集中获得的独立存储区是不同的。您可以访问不同程序集或域的存储区,方法是传入不同的程序集或域证据作为 GetStore 方法的最后两个参数。这需要访问按应用程序域标识隔离的独立存储的权限。有关更多信息,请参阅 GetStore 方法。有关程序集的更多信息,请参阅程序集。

三种方法中的每种方法都返回 IsolatedStorageFile 对象。一旦具有了独立存储文件对象之后,您便可以使用独立存储方法来读取、写入、创建和删除文件及文件目录了。

没有防止代码向没有足够访问权限来自己获取存储区的代码传递 IsolatedStorageFile 的机制。只有当获得对 IsolatedStorage 对象的引用时(通常是在 GetUserStoreForAssembly、GetUserStoreForDomain 或 GetStore 方法中),才检查域和程序集标识及独立存储权限。因此,使用这些引用的代码应该保护对 IsolatedStorageFile 对象的引用。

ObtainingAStore 示例
下面的代码示例是一个非常简单的由类获得按用户和程序集隔离的存储区的示例。通过向 GetStore 方法传递的参数添加 IsolatedStorageScope.Domain,此代码可被更改用来检索按用户、域和程序集隔离的存储区。

运行代码之后,您可以通过在命令行键入 StoreAdm /LIST 来确认已创建了存储区。这将运行独立存储管理工具 (Storeadm.exe) 并列出用户当前所有的独立存储区。

[C#]
using System;
using System.IO.IsolatedStorage;

public class ObtainingAStore{
   public static void Main(){

      // Get a new isolated store for this assembly and put it into an
      // isolated store object.

      IsolatedStorageFile isoStore =  IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

   }
}
枚举存储区
您可以使用 IsolatedStorageFile 静态方法 GetEnumerator 枚举当前用户的所有独立存储区。GetEnumerator 取 IsolatedStorageScope 值并返回 IsolatedStorageFile 枚举数。User 是唯一受支持的 IsolatedStorageScope 值。要枚举存储区,您必须具有指定 IsolatedStorageContainment 值 AdministerIsolatedStorageByUser的 IsolatedStorageFilePermission。当使用 IsolatedStorageScope 值 User 进行调用时,GetEnumerator 返回为当前用户定义的 IsolatedStorageFiles 数组。

EnumeratingStores 示例
下面的代码示例获得按用户和程序集隔离的存储区并创建几个文件。调用 GetEnumerator 方法并将结果放入 IEnumerator。然后代码依次通过 IEnumerator,添加文件的大小,并将结果报告给控制台。实际枚举发生在私有 EnumerateTheStore 方法中,为了清楚起见,将该方法与代码的其他部分分开,放在文件的底部。

[C#]
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections;

public class EnumeratingStores{

   public static int Main(){

      // Get an isolated store for this assembly and put it into an
      // IsolatedStorageFile object.

      IsolatedStorageFile isoStore =  IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

      // This code creates a few files so that they can be enumerated.

      IsolatedStorageFileStream streamA = new IsolatedStorageFileStream("TestFileA.Txt", FileMode.Create, isoStore);
      IsolatedStorageFileStream streamB = new IsolatedStorageFileStream("TestFileB.Txt", FileMode.Create, isoStore);
      IsolatedStorageFileStream streamC = new IsolatedStorageFileStream("TestFileC.Txt", FileMode.Create, isoStore);
      IsolatedStorageFileStream streamD = new IsolatedStorageFileStream("TestFileD.Txt", FileMode.Create, isoStore);

      streamA.Close();
      streamB.Close();
    &n