Export (0) Print
Expand All

How to: Anticipate Out-of-Space Conditions with Isolated Storage

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 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, the IsolatedStorage class provides three read-only properties: AvailableFreeSpace, UsedSize, and Quota. These 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 IsolatedStorage.Quota property depends on evidence from the assembly to work properly. As a result, this property should be retrieved only on IsolatedStorageFile objects that were created using the GetUserStoreForAssembly, GetUserStoreForDomain, or GetStore method. IsolatedStorageFile objects that were created in any other way (such as objects that were returned from the GetEnumerator method) will not return an accurate maximum size.

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


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);

        Console.WriteLine(isoStore.AvailableFreeSpace + " bytes of space remain in this isolated store.");
    } // End of Main.
}


Community Additions

ADD
Show:
© 2014 Microsoft