Porady: przewidywanie warunków braku miejsca w izolowanym magazynie

Kod korzystający z wydzielonego magazynu jest ograniczony przez przydział określający maksymalny rozmiar przedziału danych, w którym istnieją izolowane pliki i katalogi magazynu. Limit przydziału jest definiowany przez zasady zabezpieczeń i można go konfigurować przez administratorów. Jeśli maksymalny dozwolony rozmiar zostanie przekroczony podczas próby zapisania danych, IsolatedStorageException zgłaszany jest wyjątek i operacja kończy się niepowodzeniem. Pomaga to zapobiec złośliwym atakom typu "odmowa usługi", które mogą spowodować, że aplikacja odmówi żądań, ponieważ magazyn danych jest wypełniony.

Aby ułatwić ustalenie, czy dana próba zapisu prawdopodobnie nie powiedzie się z tego powodu, IsolatedStorage klasa zawiera trzy właściwości tylko do odczytu: AvailableFreeSpace, UsedSizei Quota. Za pomocą tych właściwości można określić, czy zapisywanie w magazynie spowoduje przekroczenie maksymalnego dozwolonego rozmiaru magazynu. Należy pamiętać, że dostęp do izolowanego magazynu można uzyskiwać jednocześnie; W związku z tym podczas obliczania ilości pozostałego miejsca do magazynowania może być zużywana przez czas próby zapisania w magazynie. Można jednak użyć maksymalnego rozmiaru magazynu, aby określić, czy zostanie osiągnięty górny limit dostępnego magazynu.

Właściwość Quota zależy od dowodów od zestawu do prawidłowego działania. Z tego powodu należy pobrać tę właściwość tylko na IsolatedStorageFile obiektach utworzonych przy użyciu GetUserStoreForAssemblymetody , GetUserStoreForDomainlub GetStore . IsolatedStorageFile obiekty utworzone w inny sposób (na przykład obiekty zwrócone z metody) nie będą zwracać dokładnego maksymalnego rozmiaru GetEnumerator .

Przykład

Poniższy przykład kodu uzyskuje izolowany magazyn, tworzy kilka plików i pobiera AvailableFreeSpace właściwość. Pozostałe miejsce jest zgłaszane w bajtach.

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

Zobacz też