使用 .NET的IO(4)

获得存储区

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

 [C#]


GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);

** GetStore ** 方法可以用于指定存储区应该和漫游用户配置文件一起漫游。

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

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

没有防止代码向没有足够访问权限来自己获取存储区的代码传递 ** IsolatedStorageFile ** 的机制。只有当获得对 IsolatedStorage 对象的引用时(通常是在 GetUserStoreForAssemblyGetUserStoreForDomainGetStore 方法中),才检查域和程序集标识及独立存储权限。因此,使用这些引用的代码应该保护对 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。当使用 IsolatedStorageScopeUser 进行调用时, 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 isoStore1Remove 方法删除用户、域和程序集存储区。然后,通过调用静态方法 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 | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);


      Console.WriteLine("A store isolated by user, assembly, and domain has been obtained.");


 


      // Get a new isolated store for user and assembly.


      // Put that store into a different IsolatedStorageFile object.


 


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


      Console.WriteLine("A store isolated by user and assembly has been obtained.");


 


      // The Remove method deletes a specific store, in this case the


      // isoStore1 file.


 


      isoStore1. **Remove();**


      Console.WriteLine("The user, domain, and assembly isolated store has been deleted.");


      


      // This static method deletes all the isolated stores for this user.


 


      **IsolatedStorageFile.Remove(IsolatedStorageScope.User);**


      Console.WriteLine("All isolated stores for this user have been deleted.");


 


   }// End of Main.


 


}

预见空间不足的情况

使用独立存储的代码受配额的限制,该配额指定独立存储文件和目录所在的数据舱的最大大小。该值由安全策略确定,管理员可以对其进行配置。如果试图写入数据时超过了所允许的最大大小,将引发 IsolatedStorageException,并使操作失败。这有助于防止恶意的抵制服务攻击,受到这种攻击后会因为数据存储被填满而导致应用程序拒绝请求。为了帮助您确定给定的写入尝试是否会因为此原因而失败,独立存储提供了两个只读属性:IsolatedStorage.CurrentSize 和 IsolatedStorage.MaximumSize。这两个属性可用于确定写入存储区是否将导致超过存储区所允许的最大大小。当您使用这些属性时,请记住独立存储可能被同时访问;因此,如果您计算的存储量有剩余,则该存储空间可能在您试图写入存储区时已被使用。但是,这不会妨碍您使用存储区的最大大小来确定是否将达到可用存储的上限。

另一个重要的考虑是最大大小属性取决于来自正常工作的程序集的证据。因此,只应该对使用 GetUserStoreForAssembly()、GetUserStoreForDomain() 或 GetStore() 创建的 IsolatedStorageFile 对象调用此方法。以其他任何方式(例如从 GetEnumerator() 中返回)创建的 IsolatedStorageFile 对象将无法返回准确的最大大小。

AnticipatingOutOfSpaceConditions 示例

下面的代码示例获得一个独立存储区,创建几个文件并度量存储区中剩余的空间。以字节数报告剩余的空间。

 [C#]


using System;


using System.IO;


using System.IO.IsolatedStorage;


 


public class CheckingSpace{


 


   public static void Main(){


 


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


      // IsolatedStoreFile object.


 


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


 


      // Create a few placeholder files in the isolated store.


 


      new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);


      new IsolatedStorageFileStream("Another.txt", FileMode.Create, isoStore);


      new IsolatedStorageFileStream("AThird.txt", FileMode.Create, isoStore);


      new IsolatedStorageFileStream("AFourth.txt", FileMode.Create, isoStore);


      new IsolatedStorageFileStream("AFifth.txt", FileMode.Create, isoStore);


 


      // Use the CurrentSize and MaximumSize methods to find remaining 


      // space.


      // Cast that number into a long type and put it into a variable.


      long spaceLeft =(long)(isoStore **.MaximumSize** - isoStore **.CurrentSize** );


 


      Console.WriteLine(spaceLeft+ " bytes of space remain in this isolated store.");


      


   }// End of Main.


 


}

创建文件和目录

获得存储区之后,您可以创建用于存储数据的目录和文件。在存储区中,文件名和目录名是相对于虚文件系统的根目录指定的。

要创建目录,请使用 IsolatedStorageFile 的 CreateDirectory 实例方法。如果您指定一个未创建目录的子目录,则会同时创建两个目录。如果您指定一个已存在的目录,将不会生成任何异常。但是,如果您指定一个包含无效字符的目录名称,则会生成 IsolatedStorageException。

要创建并打开文件,请使用 IsolatedStorageFileStream 构造函数之一,传入文件名、FileMode 值 OpenOrCreate 和要在其中创建文件的存储区。然后,您可以在文件流中对数据执行想要执行的操作,例如读取、搜索和写入。 IsolatedStorageFileStream 构造函数还可用于为其他目的打开文件。

通过使用任何不取 ** IsolatedStorageFile ** 参数的 IsolatedStorageFileStream 构造函数,您还可以在不首先获得存储区的情况下创建或打开文件。当使用这种形式的构造函数时,文件是在该文件的域存储区中创建的。

在 Windows 文件系统中,为了对名称进行比较,独立存储文件和目录名都不区分大小写。这样,如果您创建了一个名为 ThisFile.txt 的文件,然后又创建了名为 THISFILE.TXT 的另一个文件,实际上只创建了一个文件。显示时,文件名保持其原有的大小写。

CreatingFilesAndDirectories 示例

下面的代码示例阐释如何在独立存储区创建文件和目录。首先,检索一个按用户、域和程序集隔离的存储区并放入 isoStore 变量。 CreateDirectory 方法用于设置少数不同的目录,而 IsolatedStorageFileStream 方法在这些目录中创建一些文件。

 [C#]


using System;


using System.IO;


using System.IO.IsolatedStorage;


 


public class CreatingFilesDirectories{


 


   public static void Main(){


 


      // Get a new isolated store for this user, domain, and assembly.  


      // Put the store into an IsolatedStorageFile object.


 


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


 


      // This code creates a few different directories.


 


      isoStore **.CreateDirectory** ("TopLevelDirectory");


      isoStore **.CreateDirectory** ("TopLevelDirectory/SecondLevel");


 


      // This code creates two new directories, one inside the other.


      isoStore **.CreateDirectory** ("AnotherTopLevelDirectory/InsideDirectory");


 


 


      // This file is placed in the root.


 


      IsolatedStorageFileStream isoStream1 = new **IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);**


      Console.WriteLine("Created a new file in the root.");


      isoStream1.Close();


 


      // This file is placed in the InsideDirectory.


 


      IsolatedStorageFileStream isoStream2 = new **IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore);**


      isoStream2.Close();


 


      Console.WriteLine("Created a new file in the InsideDirectory.");


 


   }// End of Main.


}
Published At
Categories with Web编程
Tagged with
comments powered by Disqus