方法 : 分離ストレージを使用して領域の状態を予測する

分離ストレージを使用するコードは、分離ストレージ ファイルおよびディレクトリが格納されているデータ コンパートメントの最大サイズを指定するクォータによって制約されます。この値はセキュリティ ポリシーによって定義され、管理者が設定できます。データを書き込むときに最大許容サイズを超えると、IsolatedStorageException がスローされ、操作は失敗します。これにより、データ ストレージがいっぱいになると、アプリケーションに要求を拒否させる不当なサービス不能攻撃を防止できます。ストレージがいっぱいになると、特定の書き込みが失敗する可能性があるかどうかを判断できるように、分離ストレージには IsolatedStorage.CurrentSizeIsolatedStorage.MaximumSize の 2 つの読み取り専用プロパティが用意されています。この 2 つのプロパティを使用すると、ストアへの書き込みによってストアの最大許容サイズを超過するかどうかを確認できます。これらのプロパティを使用するときは、分離ストレージへの並列アクセスが可能なために、残りのストレージの量を計算しても、そのストアへの書き込み時点までにストレージ領域が使い果たされてしまう可能性があることを念頭に置いてください。しかし、その場合でも、ストアの最大サイズを利用して、使用可能なストレージの上限に達する時期が近いかどうかを判断することはできます。

もう 1 つ考慮する必要がある点は、最大サイズ プロパティが正しく機能するには、アセンブリからの証拠に依存するということです。このため、このメソッドは、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.

}

参照

関連項目

IsolatedStorageFile

概念

方法 : 分離ストレージでストアを取得する

その他の技術情報

分離ストレージ作業の実行