隔離存放區會在數據區間內公開虛擬文件系統。 類別 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方法的參數中傳入元件或域的證據,以存取不同元件或網域的存放區。 這需要應用程式域身分識別存取隔離記憶體的許可權。 如需詳細資訊,請參閱 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