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.
Example
The following code example obtains an isolated store, creates a few files, and retrieves the AvailableFreeSpace property. The remaining space is reported in bytes.
Imports System
Imports System.IO
Imports System.IO.IsolatedStorage
Public Class CheckingSpace
Public Shared Sub Main()
' Get an isolated store for this assembly and put it into an
' IsolatedStoreFile object.
Dim isoStore As IsolatedStorageFile = _
IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or _
IsolatedStorageScope.Assembly, 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)
Console.WriteLine(isoStore.AvailableFreeSpace + " bytes of space remain in this isolated store.")
End Sub ' End of Main.
End Class
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.
}
using namespace System;
using namespace System::IO;
using namespace System::IO::IsolatedStorage;
public ref 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, (Type ^)nullptr, (Type ^)nullptr);
// Create a few placeholder files in the isolated store.
gcnew IsolatedStorageFileStream("InTheRoot.txt", FileMode::Create, isoStore);
gcnew IsolatedStorageFileStream("Another.txt", FileMode::Create, isoStore);
gcnew IsolatedStorageFileStream("AThird.txt", FileMode::Create, isoStore);
gcnew IsolatedStorageFileStream("AFourth.txt", FileMode::Create, isoStore);
gcnew IsolatedStorageFileStream("AFifth.txt", FileMode::Create, isoStore);
Console::WriteLine(isoStore->AvailableFreeSpace + " bytes of space remain in this isolated store.");
} // End of Main.
};
int main()
{
CheckingSpace::Main();
}
See Also
Reference
Concepts
How to: Obtain Stores for Isolated Storage