作法:取得隔離儲存區的存放區

隔離存放區會公開資料區間內的虛擬檔案系統。 IsolatedStorageFile 類別提供多種與隔離存放區互動的方法。 若要建立和擷取存放區,IsolatedStorageFile 提供三種靜態方法:

  • GetUserStoreForAssembly 會傳回使用者和組件所隔離的儲存區。

  • GetUserStoreForDomain 會傳回網域和組件所隔離的儲存區。

    這兩種方法都會擷取屬於從中呼叫程式碼的存放區。

  • 靜態方法 GetStore 會傳回透過傳入範圍參數組合所指定的隔離存放區。

下列程式碼會傳回使用者、組件及網域隔離的存放區。

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)

您可以使用 GetStore 方法指定存放區應該使用漫遊使用者設定檔漫遊。 如需有關如何設定此功能的詳細資訊,請參閱隔離的類型

取自不同組件內的隔離存放區預設是不同的存放區。 您可以在 GetStore 方法的參數中傳入組件或網域辨識項,藉此存取不同組件或網域的存放區。 這需要依應用程式網域身分識別存取隔離儲存區的權限。 如需詳細資訊,請參閱 GetStore 方法多載。

GetUserStoreForAssemblyGetUserStoreForDomainGetStore 方法會傳回 IsolatedStorageFile 物件。 若要協助您決定最適合您情況的隔離類型,請參閱隔離的類型。 當您擁有隔離儲存區檔案物件時,可以使用隔離儲存區方法讀取、寫入、建立以及刪除檔案和目錄。

沒有任何機制可防止程式碼將 IsolatedStorageFile 物件傳遞至沒有足夠存取權可取得存放區本身的程式碼。 只有在取得 IsolatedStorage 物件的參考 (通常是在 GetUserStoreForAssemblyGetUserStoreForDomainGetStore 方法中) 時,才會檢查網域和組件的身分識別和隔離儲存區權限。 因此,保護 IsolatedStorageFile 物件的參考是使用這些參考之程式碼的責任。

範例

下列程式碼可針對取得使用者和組件所隔離之存放區的類別,提供簡單的範例。 您可以變更程式碼,以便透過將 IsolatedStorageScope.Domain 新增至 GetStore 方法所傳遞的引數,擷取使用者、網域和組件隔離的存放區。

執行程式碼之後,您可以確認已透過在命令列中輸入 StoreAdm /LIST 來建立存放區。 如此會執行隔離儲存區工具 (Storeadm.exe) ,並列出目前使用者的所有隔離存放區。

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.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

另請參閱