Dela via


How to: Obtain Stores for Isolated Storage

A store exposes a virtual file system within a data compartment. IsolatedStorageFile supplies a number of methods for interacting with a store. To create and retrieve stores, IsolatedStorageFile provides three static methods. Calling GetUserStoreForAssembly returns storage isolated by user and assembly. Calling GetUserStoreForDomain returns storage isolated by domain and assembly. These two methods retrieve a store that belongs to the block of code from which they are called. The static method GetStore returns an isolated store that is specified by passing in a combination of scope parameters. The following parameters return a store isolated by user, assembly, and domain.

Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or
    IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
    IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);
IsolatedStorageFile^ isoStore = IsolatedStorageFile::GetStore(IsolatedStorageScope::User |
    IsolatedStorageScope::Assembly | IsolatedStorageScope::Domain, (Type ^)nullptr, (Type ^)nullptr);

The GetStore method can be used to specify that a store should roam with a roaming user profile. For details on how to set this up, see Isolated Storage and Roaming.

Isolated stores obtained from within different assemblies are, by default, different stores. You can access the store of a different assembly or domain by passing in different assembly or domain evidence as the last two parameters of the GetStore method. This requires permission to access isolated storage by application domain identity. For more information, see the GetStore method.

Each of these three methods returns an IsolatedStorageFile object. To help you decide which isolation type is most appropriate for your situation, see Types of Isolation. Once you have an isolated storage file object, you can use the isolated storage methods to read, write, create, and delete files and file directories.

There is no mechanism that prevents code from passing an IsolatedStorageFile to code that does not have sufficient access to get the store itself. Domain and assembly identities and isolated storage permissions are checked only when a reference to an IsolatedStorage object is obtained, typically in the GetUserStoreForAssembly, GetUserStoreForDomain, or GetStore method. Protecting references to IsolatedStorageFile objects is, therefore, the responsibility of the code that uses these references.

ObtainingAStore Example

The following code example is a very simple example of a class obtaining a store isolated by user and assembly. The code can be changed to retrieve a store isolated by user, domain, and assembly by adding IsolatedStorageScope.Domain to the arguments that the GetStore method is passing.

After running the code, you can confirm that a store was created by typing StoreAdm /LIST at the command line. This runs the Isolated Storage administration tool (Storeadm.exe) and lists all the current isolated stores for the user. For more information about Storeadm.exe, see Isolated Storage Tool.

Imports System
Imports System.IO.IsolatedStorage

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

        Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or
            IsolatedStorageScope.Assembly, Nothing, Nothing)
    End Sub
End Class
using System;
using System.IO.IsolatedStorage;

public class ObtainingAStore
{
    public static void Main()
    {
        // Get a new isolated store for this assembly and put it into an
        // isolated store object.

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly, null, null);
    }
}
using namespace System;
using namespace System::IO::IsolatedStorage;

public ref class ObtainingAStore
{
public:
    static void Main()
    {
        // Get a new isolated store for this assembly and put it into an
        // isolated store object.

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

See Also

Reference

IsolatedStorageFile

IsolatedStorageScope

Concepts

Types of Isolation

Other Resources

Isolated Storage

Assemblies in the Common Language Runtime