Anticipating Out of Space Conditions

Code that uses isolated storage is constrained by a quota that specifies the maximum size for the data compartment in which isolated storage files and directories exist. This value is defined by security policy and is configurable by administrators. If the maximum allowed size is exceeded when an attempt is made to write data, an IsolatedStorageException is thrown and the operation fails. This helps to prevent malicious denial of service attacks that could cause the application to refuse requests because data storage is filled. To help you determine whether a given write attempt is likely to fail for this reason, isolated storage provides two read-only properties: IsolatedStorage.CurrentSize and IsolatedStorage.MaximumSize. These two properties can be used to determine whether writing to the store will cause the maximum allowed size of the store to be exceeded. When you use these properties, keep in mind that isolated storage can be concurrently accessed; therefore, if you compute the amount of storage remaining, the storage space could be consumed by the time you attempt to write to the store. However, this doesn't prevent you from using the maximum size of the store to help determine whether the upper limit on available storage is about to be reached.

Another important consideration is that the maximum size property depends on evidence from the assembly to work properly. As a result, this method should only be called on IsolatedStorageFile objects created using GetUserStoreForAssembly(), GetUserStoreForDomain(), or GetStore(). IsolatedStorageFile objects that have been created in any other way (such as being returned from GetEnumerator()) will not return an accurate maximum size.

AnticipatingOutOfSpaceConditions Example

The following code example obtains an isolated store, creates a few files, and measures the space remaining in the store. The space remaining is reported in bytes.

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

[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.

}

See Also

Performing Isolated Storage Tasks | IsolatedStorageFile | Obtaining a Store