HOW TO:在隔離儲存區中建立檔案和目錄
更新:2007 年 11 月
在您取得存放區之後,您可以建立儲存資料的目錄和檔案。存放區內,檔案和目錄名稱相對於虛擬檔案系統的根 (Root) 來指定。
如果要建立目錄,請使用 IsolatedStorageFile 的 CreateDirectory 執行個體方法。如果您指定尚未建立的目錄的子目錄,那麼兩個目錄都會建立。如果您指定已經存在的目錄,不會產生任何例外狀況。但是,如果您指定的目錄名稱內含無效字元,則會產生 IsolatedStorageException。
如果要建立並開啟檔案,請使用其中一個 IsolatedStorageFileStream 建構函式,傳入檔案名稱、FileMode 值 OpenOrCreate,以及您想要在其中建立檔案的存放區。然後,您可以做一些預期要在檔案資料流中處理資料的動作,例如讀取、搜尋和寫入。IsolatedStorageFileStream 建構函式也可以用來開啟其他用途的檔案。
您可以使用不接受 IsolatedStorageFile 引數的任何 IsolatedStorageFileStream 建構函式,建立或開啟檔案而不需事先取得存放區。當您使用這個形式的建構函式時,檔案是建立在檔案的定義域存放區中。
在 Windows 檔案系統中,為了比較名稱,隔離儲存區的檔案和目錄名稱會區分大小寫。如此,如果您建立名為 ThisFile.txt 的檔案,並接著建立另一個名為 THISFILE.TXT 的檔案,只有一個檔案會被建立。檔案名稱在顯示的場合保留它原來的大小寫。
CreatingFilesAndDirectories 範例
下列程式碼範例說明如何在隔離存放區中建立檔案和目錄。首先,擷取使用者、定義域和組件所隔離的存放區,並置於 isoStore 變數中。CreateDirectory 方法是用來設定一些不同的目錄,而 IsolatedStorageFileStream 方法則會在這些目錄中建立一些檔案。
Imports System
Imports System.IO
Imports System.IO.IsolatedStorage
Public Module modmain
Sub Main()
' Get an isolated store for user, domain, and assembly and put it into
' an IsolatedStorageFile object.
Dim isoStore As IsolatedStorageFile
isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)
' This code creates a few different directories.
isoStore.CreateDirectory("TopLevelDirectory")
isoStore.CreateDirectory("TopLevelDirectory/SecondLevel")
' This code creates two new directories, one inside the other.
isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory")
' This file is placed in the root.
Dim isoStream1 As New IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore)
Console.WriteLine("Created a new file in the root.")
isoStream1.Close()
' This file is placed in the InsideDirectory.
Dim isoStream2 As New IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore)
Console.WriteLine("Created a new file in the root.")
isoStream2.Close()
Console.WriteLine("Created a new file in the InsideDirectory.")
End Sub
End Module
using System;
using System.IO;
using System.IO.IsolatedStorage;
public class CreatingFilesDirectories{
public static void Main(){
// Get a new isolated store for this user, domain, and assembly.
// Put the store into an IsolatedStorageFile object.
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);
// This code creates a few different directories.
isoStore.CreateDirectory("TopLevelDirectory");
isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");
// This code creates two new directories, one inside the other.
isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
// This file is placed in the root.
IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
Console.WriteLine("Created a new file in the root.");
isoStream1.Close();
// This file is placed in the InsideDirectory.
IsolatedStorageFileStream isoStream2 = new IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore);
isoStream2.Close();
Console.WriteLine("Created a new file in the InsideDirectory.");
}// End of Main.
}