如何:预见独立存储中的空间不足情况

更新:2007 年 11 月

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

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

AnticipatingOutOfSpaceConditions 示例

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

Imports System
Imports System.IO
Imports System.IO.IsolatedStorage

Public Module modmain

   Sub Main()

      ' Get an isolated store for user, domain, and assembly and put it into 
      ' an IsolatedStorageFile object.

      Dim isoStore As IsolatedStorageFile
      isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)

      ' Create a few placeholder files in the isolated store.

      Dim aStream As New IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore)
      Dim bStream As New IsolatedStorageFileStream("Another.txt", FileMode.Create, isoStore)
      Dim cStream As New IsolatedStorageFileStream("AThird.txt", FileMode.Create, isoStore)
      Dim dStream As New IsolatedStorageFileStream("AFourth.txt", FileMode.Create, isoStore)
      Dim eStream As 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.

      Dim spaceLeft As Long
      spaceLeft = CLng((isoStore.MaximumSize.ToString) - isoStore.CurrentSize.ToString)

      Console.WriteLine("CurrentSize: " + isoStore.CurrentSize.ToString)

      Console.WriteLine("Space Left: " + spaceLeft.ToString + " (might be very large if no quota is set)")

   End Sub
End Module
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

其他资源

执行独立存储任务