作法:預期隔離儲存區發生空間不足的情況

使用隔離儲存區的程式碼受到配額的限制,此限制可針對隔離儲存區檔案和目錄所在的資料區間,指定其大小上限。 配額由安全性原則所定義且由系統管理員設定。 當您嘗試寫入資料時,如果超過允許的大小上限,則會擲回 IsolatedStorageException 例外狀況,且作業會失敗。 這有助於防止惡意的拒絕服務攻擊,此功能可能會因為資料存放區已滿而導致應用程式拒絕要求。

為協助您判斷指定的寫入嘗試是否可能因為這個原因而失敗,IsolatedStorage 類別會提供三個唯讀屬性:AvailableFreeSpaceUsedSizeQuota。 您可以使用這些屬性判斷寫入存放區是否會導致超過存放區允許的大小上限。 請記住,您可以並行存取隔離儲存區。因此,在計算剩餘的儲存區數量時,可能會在您嘗試寫入存放區時耗用儲存空間。 不過,您可以使用存放區的大小上限,以協助判斷是否即將到達可用儲存空間的上限。

Quota 屬性相依於組件的辨識項,才能正常運作。 因此,您僅能針對使用 GetUserStoreForAssemblyGetUserStoreForDomainGetStore 方法建立的 IsolatedStorageFile 物件,擷取此屬性。 以其他任何方式所建立的 IsolatedStorageFile 物件 (例如,從 GetEnumerator 方法傳回的物件) 將不會傳回精確的大小上限。

範例

下列程式碼範例會取得隔離儲存區、建立一些檔案,然後擷取 AvailableFreeSpace 屬性。 剩餘的空間會以位元組為單位來報告。

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();
}
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.
}
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 Class

另請參閱