Share via


方法: 分離ストレージの領域不足状態に備える

更新 : 2010 年 12 月

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

もう 1 つ考慮する必要がある点は、IsolatedStorage.Quota プロパティが正しく機能するには、アセンブリからの証拠に依存するということです。 そのため、このプロパティは、GetUserStoreForAssemblyGetUserStoreForDomain、または GetStore のメソッドで作成された 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

概念

分離ストレージ

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

履歴の変更

日付

履歴

理由

2010 年 12 月

AvailableFreeSpace プロパティに関する情報を追加。

情報の拡充