如何:在独立存储中创建文件和目录
获得存储区之后,您可以创建用于存储数据的目录和文件。 在存储区中,文件名和目录名是相对于虚文件系统的根目录指定的。
若要创建目录,请使用 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 Class CreatingFilesDirectories
Public Shared 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 Class
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.
}
using namespace System;
using namespace System::IO;
using namespace System::IO::IsolatedStorage;
public ref 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,
(Type ^)nullptr, (Type ^)nullptr);
// 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 =
gcnew 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 =
gcnew IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt",
FileMode::Create, isoStore);
isoStream2->Close();
Console::WriteLine("Created a new file in the InsideDirectory.");
} // End of Main.
};
int main()
{
CreatingFilesDirectories::Main();
}