独立存储会在数据隔间内公开虚拟文件系统。 该 IsolatedStorageFile 类提供了许多用于与独立存储进行交互的方法。 要创建和检索存储,IsolatedStorageFile 提供了三种静态方法:
GetUserStoreForAssembly 返回由用户和程序集隔离的存储。
GetUserStoreForDomain 返回由域和程序集隔离的存储。
这两种方法都检索与调用它们的代码关联的存储。
静态方法 GetStore 返回通过传入范围参数组合指定的独立存储。
以下代码返回由用户、程序集和域隔离的存储。
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)
可以使用该方法 GetStore 指定应用商店应使用漫游用户配置文件漫游。 有关如何设置此设置的详细信息,请参阅 隔离类型。
从不同程序集内获取的隔离存储默认情况下是不同的存储。 可以通过在方法的参数中传入程序集或域的证据来访问其他程序集或域的存储。 这需要有权通过应用程序域标识访问独立存储。 有关详细信息,请参阅 GetStore 方法重载。
GetUserStoreForAssembly、GetUserStoreForDomain和GetStore方法返回一个IsolatedStorageFile对象。 若要帮助你确定哪种隔离类型最适合你的情况,请参阅 隔离类型。 拥有独立存储文件对象时,可以使用隔离存储方法读取、写入、创建和删除文件和目录。
没有机制可以阻止代码将 IsolatedStorageFile 对象传递给没有足够权限访问存储的代码。 仅在获取对IsolatedStorage对象的引用时(通常发生在GetUserStoreForAssembly、GetUserStoreForDomain或GetStore方法中),才会检查域和程序集标识以及独立存储权限。 因此,保护对 IsolatedStorageFile 对象的引用是使用这些引用的代码的责任。
示例:
下面的代码提供了一个类的简单示例,该类获取由用户和程序集隔离的存储区。 可以通过在 IsolatedStorageScope.Domain 方法传递的参数中添加 GetStore 来更改代码,以检索由用户、域和组件隔离的存储。
运行代码后,可以通过在命令行键入 StoreAdm /LIST 来确认已创建存储。 这将运行 独立存储工具(Storeadm.exe), 并列出用户的所有当前独立存储。
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.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