HOW TO:預期隔離儲存區發生空間不足的情況
更新:2007 年 11 月
使用隔離儲存區的程式碼受限於指定隔離儲存區檔案和目錄所存在資料區間最大大小的隔離儲存區的配額。這個值由安全性原則定義,並可由系統管理員設定。如果嘗試寫入資料時超過最大容許大小,將會擲回 IsolatedStorageException,且作業失敗。這有助防止惡性的拒絕服務攻擊,以免造成應用程式因為資料儲存區被填滿而拒絕要求。為了幫助您判斷某個寫入嘗試是否可能會因此失敗,隔離的存放區提供兩個唯讀屬性:IsolatedStorage.CurrentSize 和 IsolatedStorage.MaximumSize。這兩個屬性可以用來判斷對存放區的寫入是否將造成存放區的最大容許大小被超出。當您使用這些屬性時,請記得,隔離儲存區可並行存取;因此,如果要計算儲存區的剩餘數量,在您嘗試寫入存放區前,可能又會消耗掉一些儲存空間。然而,這不會阻止您使用存放區的最大大小來協助判斷是否即將到達可用儲存區的上限。
另一個重要的考量是,得以正確工作的最大大小的屬性取決於來自組件的辨識項。因此,這個方法只能在使用 GetUserStoreForAssembly()、GetUserStoreForDomain() 或 GetStore() 建立的 IsolatedStorageFile 物件上呼叫。用其他任何方式 (例如從 GetEnumerator() 傳回來) 建立的 IsolatedStorageFile 物件將不會傳回正確的最大值。
AnticipatingOutOfSpaceConditions 範例
下列程式碼範例取得隔離存放區、建立一些檔案,並測量存放區中剩餘的空間。剩餘空間將以位元組來報告。
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
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.
}