IsolatedStorageScope.Assembly, null, null);
// Create a few placeholder files in the isolated store.
new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
new IsolatedStorageFileStream("Another.txt", FileMode.Create, isoStore);
new IsolatedStorageFileStream("AThird.txt", FileMode.Create, isoStore);
new IsolatedStorageFileStream("AFourth.txt", FileMode.Create, isoStore);
new IsolatedStorageFileStream("AFifth.txt", FileMode.Create, isoStore);
// Use the CurrentSize and MaximumSize methods to find remaining
// space.
// Cast that number into a long type and put it into a variable.
long spaceLeft =(long)(isoStore.MaximumSize - isoStore.CurrentSize);
Console.WriteLine(spaceLeft+ " bytes of space remain in this isolated store.");
}// End of Main.
}
创建文件和目录
获得存储区之后,您可以创建用于存储数据的目录和文件。在存储区中,文件名和目录名是相对于虚文件系统的根目录指定的。
要创建目录,请使用 IsolatedStorageFile 的 CreateDirectory 实例方法。如果您指定一个未创建目录的子目录,则会同时创建两个目录。如果您指定一个已存在的目录,将不会生成任何异常。但是,如果您指定一个包含无效字符的目录名称,则会生成 IsolatedStorageException。
要创建并打开文件,请使用 IsolatedStorageFileStream 构造函数之一,传入文件名、FileMode 值 OpenOrCreate 和要在其中创建文件的存储区。然后,您可以在文件流中对数据执行想要执行的操作,例如读取、搜索和写入。IsolatedStorageFileStream 构造函数还可用于为其他目的打开文件。
通过使用任何不取 IsolatedStorageFile 参数的 IsolatedStorageFileStream 构造函数,您还可以在不首先获得存储区的情况下创建或打开文件。当使用这种形式的构造函数时,文件是在该文件的域存储区中创建的。
在 Windows 文件系统中,为了对名称进行比较,独立存储文件和目录名都不区分大小写。这样,如果您创建了一个名为 ThisFile.txt 的文件,然后又创建了名为 THISFILE.TXT 的另一个文件,实际上只创建了一个文件。显示时,文件名保持其原有的大小写。
CreatingFilesAndDirectories 示例
下面的代码示例阐释如何在独立存储区创建文件和目录。首先,检索一个按用户、域和程序集隔离的存储区并放入 isoStore 变量。CreateDirectory 方法用于设置少数不同的目录,而 IsolatedStorageFileStream 方法在这些目录中创建一些文件。
[C#]
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
关键词:运用 .NET的IO(4) Paul_Ni(原作)