当前位置:编程学习 > C#/ASP.NET >>

使用 .NET的IO(4) Paul_Ni(原作)

答案:获得存储区
存储区公开数据舱中的虚文件系统。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();
      streamC.Close();
      streamD.Close();

      // There might be a small delay between when the above code
      // executes and when the files are created in the store.
      // Closing and opening the store in this example ensures that
      // the common language runtime has finished creating the files.
      isoStore .Close();
      isoStore =  IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
     
      
      // This line of code calls a method at the bottom of the program
      // that puts all the files in isoStore into an IEnumerator.

      IEnumerator allFiles = EnumerateTheStore (isoStore);
      long totalsize = 0;

      // This code counts up the sizes of all the stores.

      while(allFiles .MoveNext()){
         IsolatedStorageFile store = (IsolatedStorageFile)allFiles.Current;
         totalsize += (long)store.CurrentSize;
      }

      Console.WriteLine("The total size = "+totalsize);
      return 0;
   }

   // This method returns an IEnucontaining all the files for a user.

   private static IEnumerator EnumerateTheStore(IsolatedStorageFile isoStore){
      
      IEnumerator e = IsolatedStorageFile.GetEnumerator(IsolatedStorageScope.User);
      
      return e;
   }
}
删除存储区
IsolatedStorageFile 提供了两种删除独立存储文件的方法:

实例方法 Remove 不取任何参数,删除调用它的存储区。该操作不需要任何权限。可以访问存储区的任何代码都可以删除该存储区中的任何数据或所有数据。
静态方法 Remove 采用 IsolatedStorageScope 值 User,并删除运行该代码的用户的所有存储区。该操作需要 IsolatedStorageContainment 值 AdministerIsolatedStorageByUser 的 IsolatedStorageFilePermission 权限。
DeletingStores 示例
下面的代码示例演示了静态和实例 Remove 方法的使用。类获得两个存储区,一个按用户和程序集隔离;另一个按用户、域和程序集隔离。通过调用 IsolatedStorageFile isoStore1 的 Remove 方法删除用户、域和程序集存储区。然后,通过调用静态方法 IsolatedStorageFile.Remove 删除该用户所有剩余的存储区。

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

public class DeletingStores{

   public static void Main(){

      // Get a new isolated store for this user, domain, and assembly.
      // Put the store into an IsolatedStorageFile object.

      IsolatedStorageFile isoStore1 =  IsolatedStorageFile.GetStore(IsolatedStorageScope.User |

上一个:在.NET运行时了解类型信息(1) Paul_Ni(原作)
下一个:使用 .NET的IO(5) Paul_Ni(原作)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,