How to: Obtain Stores for Isolated Storage

An isolated store exposes a virtual file system within a data compartment. The IsolatedStorageFile class supplies a number of methods for interacting with an isolated store. To create and retrieve stores, IsolatedStorageFile provides three static methods:

  • GetUserStoreForAssembly returns storage that is isolated by user and assembly.

  • GetUserStoreForDomain returns storage that is isolated by domain and assembly.

    Both methods retrieve a store that belongs to the 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 code returns a store that is isolated by user, assembly, and domain.

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

You can use the GetStore method to specify that a store should roam with a roaming user profile. For details on how to set this up, see Types of Isolation.

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 the assembly or domain evidence in the parameters of the GetStore method. This requires permission to access isolated storage by application domain identity. For more information, see the GetStore method overloads.

The GetUserStoreForAssembly, GetUserStoreForDomain, and GetStore methods return an IsolatedStorageFile object. To help you decide which isolation type is most appropriate for your situation, see Types of Isolation. When you have an isolated storage file object, you can use the isolated storage methods to read, write, create, and delete files and directories.

There is no mechanism that prevents code from passing an IsolatedStorageFile object 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.

Example

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

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

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);
    }
};
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);
    }
}
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

See Also

IsolatedStorageFile
IsolatedStorageScope
Isolated Storage
Types of Isolation
Assemblies in the Common Language Runtime