Поделиться через


Практическое руководство. Перечисление хранилищ для изолированного хранилища

Перечислить все изолированные хранилища для текущего пользователя можно с помощью IsolatedStorageFile статического метода GetEnumerator. Метод GetEnumerator принимает значение IsolatedStorageScope и возвращает перечислитель IsolatedStorageFile. Значение User — это единственное значение, поддерживаемое IsolatedStorageScope. Чтобы перечислить хранилища, необходимо иметь IsolatedStorageFilePermission, который указывает IsolatedStorageContainment значение AdministerIsolatedStorageByUser. При вызове IsolatedStorageScope со значением User, метод GetEnumerator возвращает массив файлов IsolatedStorageFiles, определенных для текущего пользователя.

Пример EnumeratingStores

В следующем примере получаются хранилища, изолированные по пользователю и сборке, и создается несколько файлов. Вызывается метод GetEnumerator и результат помещается в IEnumerator. Затем код реализует цикл по IEnumerator, складывая размеры файлов и выводит результат на консоль. Собственно перечисление происходит в закрытом методе EnumerateTheStore, который для большей ясности приведен отдельно в конце файла.

Imports System
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Collections

Public Class EnumeratingStores
    Public Shared Sub Main()
        ' Get an isolated store for this assembly and put it into an
        ' IsolatedStorageFile object.

        Dim isoStore As IsolatedStorageFile = _
            IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or _
            IsolatedStorageScope.Assembly, Nothing, Nothing)

        ' This code creates a few files so that they can be enumerated.

        Dim streamA As New IsolatedStorageFileStream("TestFileA.Txt", FileMode.Create, isoStore)
        Dim streamB As New IsolatedStorageFileStream("TestFileB.Txt", FileMode.Create, isoStore)
        Dim streamC As New IsolatedStorageFileStream("TestFileC.Txt", FileMode.Create, isoStore)
        Dim streamD As New IsolatedStorageFileStream("TestFileD.Txt", FileMode.Create, isoStore)

        streamA.Close()
        streamB.Close()
        streamC.Close()
        streamD.Close()

        ' There might be a small delay between when the above code
        ' executes and when the files are created in the store.
        ' Closing and opening the store in this example ensures that
        ' the common language runtime has finished creating the files.
        isoStore .Close()
        isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or _
            IsolatedStorageScope.Assembly, Nothing, Nothing)

        ' This line of code calls a method at the bottom of the program
        ' that puts all the files in isoStore into an IEnumerator.

        Dim allFiles As IEnumerator = EnumerateTheStore (isoStore)
        Dim totalsize As Long = 0

        ' This code counts up the sizes of all the stores.

        while(allFiles .MoveNext())
            Dim store As IsolatedStorageFile
            store = CType(allFiles.Current, IsolatedStorageFile)
            totalsize += CType(store.UsedSize, Long)
        End While

        Console.WriteLine("The total size = " + totalsize)
    End Sub

    ' This method returns an enumerator of all the files for a user.

    Private Shared Function EnumerateTheStore(isoStore As IsolatedStorageFile) As IEnumerator
        Dim e As IEnumerator = IsolatedStorageFile.GetEnumerator(IsolatedStorageScope.User)

        Return e
    End Function
End Class
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections;

public class EnumeratingStores
{
    public static int Main()
    {
        // Get an isolated store for this assembly and put it into an
        // IsolatedStorageFile object.

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly, null, null);

        // This code creates a few files so that they can be enumerated.

        IsolatedStorageFileStream streamA = new IsolatedStorageFileStream("TestFileA.Txt", FileMode.Create, isoStore);
        IsolatedStorageFileStream streamB = new IsolatedStorageFileStream("TestFileB.Txt", FileMode.Create, isoStore);
        IsolatedStorageFileStream streamC = new IsolatedStorageFileStream("TestFileC.Txt", FileMode.Create, isoStore);
        IsolatedStorageFileStream streamD = new IsolatedStorageFileStream("TestFileD.Txt", FileMode.Create, isoStore);

        streamA.Close();
        streamB.Close();
        streamC.Close();
        streamD.Close();

        // There might be a small delay between when the above code
        // executes and when the files are created in the store.
        // Closing and opening the store in this example ensures that
        // the common language runtime has finished creating the files.
        isoStore .Close();
        isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly, null, null);

        // This line of code calls a method at the bottom of the program
        // that puts all the files in isoStore into an IEnumerator.

        IEnumerator allFiles = EnumerateTheStore (isoStore);
        long totalsize = 0;

        // This code counts up the sizes of all the stores.

        while(allFiles .MoveNext())
        {
            IsolatedStorageFile store = (IsolatedStorageFile)allFiles.Current;
            totalsize += (long)store.UsedSize;
        }

        Console.WriteLine("The total size = "+totalsize);
        return 0;
    }

    // This method returns an enumerator of all the files for a user.

    private static IEnumerator EnumerateTheStore(IsolatedStorageFile isoStore)
    {
        IEnumerator e = IsolatedStorageFile.GetEnumerator(IsolatedStorageScope.User);

        return e;
    }
}
using namespace System;
using namespace System::IO;
using namespace System::IO::IsolatedStorage;
using namespace System::Collections;

public ref class EnumeratingStores
{
public:
    static int Main()
    {
        // Get an isolated store for this assembly and put it into an
        // IsolatedStorageFile object.

        IsolatedStorageFile^ isoStore = IsolatedStorageFile::GetStore(IsolatedStorageScope::User |
            IsolatedStorageScope::Assembly, (Type ^)nullptr, (Type ^)nullptr);

        // This code creates a few files so that they can be enumerated.

        IsolatedStorageFileStream^ streamA =
            gcnew IsolatedStorageFileStream("TestFileA.Txt", FileMode::Create, isoStore);
        IsolatedStorageFileStream^ streamB =
            gcnew IsolatedStorageFileStream("TestFileB.Txt", FileMode::Create, isoStore);
        IsolatedStorageFileStream^ streamC =
            gcnew IsolatedStorageFileStream("TestFileC.Txt", FileMode::Create, isoStore);
        IsolatedStorageFileStream^ streamD =
            gcnew IsolatedStorageFileStream("TestFileD.Txt", FileMode::Create, isoStore);

        streamA->Close();
        streamB->Close();
        streamC->Close();
        streamD->Close();

        // There might be a small delay between when the above code
        // executes and when the files are created in the store.
        // Closing and opening the store in this example ensures that
        // the common language runtime has finished creating the files.
        isoStore->Close();
        isoStore = IsolatedStorageFile::GetStore(IsolatedStorageScope::User |
            IsolatedStorageScope::Assembly, (Type ^)nullptr, (Type ^)nullptr);

        // This line of code calls a method at the bottom of the program
        // that puts all the files in isoStore into an IEnumerator.

        IEnumerator^ allFiles = EnumerateTheStore(isoStore);
        long totalsize = 0;

        // This code counts up the sizes of all the stores.

        while (allFiles->MoveNext())
        {
            IsolatedStorageFile^ store = (IsolatedStorageFile^)allFiles->Current;
            totalsize += (long)store->UsedSize;
        }

        Console::WriteLine("The total size = " + totalsize);
        return 0;
    }

    // This method returns an enumerator of all the files for a user.

private:
    static IEnumerator^ EnumerateTheStore(IsolatedStorageFile^ isoStore)
    {
        IEnumerator^ e = IsolatedStorageFile::GetEnumerator(IsolatedStorageScope::User);

        return e;
    }
};

int main()
{
    EnumeratingStores::Main();
}

См. также

Ссылки

IsolatedStorageFile

Основные понятия

Изолированное хранилище