共用方式為


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

更新:2010 年 12 月

使用隔離儲存區的程式碼受限於指定隔離儲存區檔案和目錄所存在資料區間最大大小的隔離儲存區的配額。 這個值由安全性原則定義,並可由系統管理員設定。 如果嘗試寫入資料時超過最大容許大小,將會擲回 IsolatedStorageException,且作業失敗。 這樣有助於防止惡意的拒絕服務攻擊,以免造成應用程式因為資料儲存區被填滿而拒絕要求。 為了幫助您判斷某個寫入嘗試是否可能會因此失敗,IsolatedStorage 類別提供了三個唯讀屬性:AvailableFreeSpaceUsedSizeQuota。 這些屬性可以用來判斷對存放區的寫入是否將造成超出存放區的最大容許大小。 當您使用這些屬性時,請記得,隔離儲存區可並行存取;因此,如果要計算儲存區的剩餘數量,在您嘗試寫入存放區前,可能又會消耗掉一些儲存空間。 然而,這不會阻止您使用存放區的最大大小來協助判斷是否即將到達可用儲存區的上限。

另一項重要的考量是,IsolatedStorage.Quota 屬性是否倚賴來自組件的辨識項才能正確運作。 因此,這個屬性應該只在使用 GetUserStoreForAssemblyGetUserStoreForDomainGetStore 方法的 IsolatedStorageFile 物件上擷取。 以任何其他方式建立的 IsolatedStorageFile 物件 (例如,從 GetEnumerator 方法傳回的物件) 將不會傳回正確的最大值。

範例

下列程式碼範例會取得隔離的存放區、建立幾個檔案,並且擷取 AvailableFreeSpace 屬性。 剩餘空間將以位元組為單位回報。

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

請參閱

參考

IsolatedStorageFile

概念

隔離儲存區

HOW TO:取得離儲存區的存放區

變更記錄

日期

記錄

原因

2010 年 12 月

已加入關於 AvailableFreeSpace 屬性的資訊。

資訊加強。