.NET Framework 文件 I/O 和文件系统基础知识 (Visual Basic)Basics of .NET Framework File I/O and the File System (Visual Basic)

使用 System.IO 命名空间中的类与驱动器、文件和目录一起工作。Classes in the System.IO namespace are used to work with drives, files, and directories.

System.IO 命名空间包含 FileDirectory 类,它们提供用于操作文件和目录的 .NET Framework 功能。The System.IO namespace contains the File and Directory classes, which provide the .NET Framework functionality that manipulates files and directories. 由于这些对象的方法是静态或共享成员,因此可直接使用,无需首先创建类的实例。Because the methods of these objects are static or shared members, you can use them directly without creating an instance of the class first. 与这些类相关联的是 FileInfoDirectoryInfo 类,使用 My 功能的用户将对它们很熟悉。Associated with these classes are the FileInfo and DirectoryInfo classes, which will be familiar to users of the My feature. 若要使用这些类,必须通过在受影响的代码开头包含 Imports 语句,完全限定名称或导入相应的命名空间。To use these classes, you must fully qualify the names or import the appropriate namespaces by including the Imports statement(s) at the beginning of the affected code. 有关详细信息,请参阅 Imports 语句(.NET 命名空间和类型)For more information, see Imports Statement (.NET Namespace and Type).

备注

此部分中的其他主题使用 My.Computer.FileSystem 对象,而不使用 System.IO 类与驱动器、文件和目录一起工作。Other topics in this section use the My.Computer.FileSystem object instead of System.IO classes to work with drives, files, and directories. My.Computer.FileSystem 对象的主要目的是用在 Visual Basic 程序中。The My.Computer.FileSystem object is intended primarily for use in Visual Basic programs. System.IO 类旨在供任何支持 .NET Framework(包括 Visual Basic)的语言使用。System.IO classes are intended for use by any language that supports the .NET Framework, including Visual Basic.

流的定义Definition of a Stream

.NET Framework 使用流来支持从文件中读取和写入文件。The .NET Framework uses streams to support reading from and writing to files. 可以将流视为一维连续数据集,具有开始和结束,并且其中的游标指示流中的当前位置。You can think of a stream as a one-dimensional set of contiguous data, which has a beginning and an end, and where the cursor indicates the current position in the stream.

光标显示了 Filestream 中的当前位置。

流操作Stream Operations

流中包含的数据可能来自内存、文件或 TCP/IP 套接字。The data contained in the stream may come from memory, a file, or a TCP/IP socket. 流具有可应用于它们的基本操作:Streams have fundamental operations that can be applied to them:

  • 读取Reading. 可以从流读取数据,将数据从流传输到数据结构,如字符串或字节数组。You can read from a stream, transferring data from the stream into a data structure, such as a string or an array of bytes.

  • 编写Writing. 可以将数据写入流中,将数据从数据源传输到流。You can write to a stream, transferring data from a data source into the stream.

  • 查找Seeking. 可以查询和修改流中的位置。You can query and modify your position in the stream.

有关详细信息,请参阅 “撰写流”。For more information, see Composing Streams.

流的类型Types of Streams

在 .NET Framework 中,流由 Stream 类表示,这形成了所有其他流的抽象类。In the .NET Framework, a stream is represented by the Stream class, which forms the abstract class for all other streams. 不能直接创建 Stream 类的实例,但必须使用它执行的一个类。You cannot directly create an instance of the Stream class, but must use one of the classes it implements.

存在许多类型的流,但就处理文件输入/输出 (I/O) 而言,最重要的类型是 FileStream 类(提供从文件读取和写入文件的方式)和 IsolatedStorageFileStream 类(提供在独立存储中创建文件和目录的方法)。There are many types of streams, but for the purposes of working with file input/output (I/O), the most important types are the FileStream class, which provides a way to read from and write to files, and the IsolatedStorageFileStream class, which provides a way to create files and directories in isolated storage. 处理文件 I/O 时可使用的其他流包括:Other streams that can be used when working with file I/O include:

下表列出了通常使用流完成的任务:The following table lists tasks commonly accomplished with a stream:

To 请参阅See
读取和写入数据文件Read and write to a data file 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File
从文件中读取文本Read text from a file 如何:从文件读取文本How to: Read Text from a File
将文本写入文件Write text to a file 如何:向文件写入文本How to: Write Text to a File
从字符串中读取字符Read characters from a string 如何:从字符串中读取字符How to: Read Characters from a String
向字符串写入字符Write characters to a string 如何:向字符串写入字符How to: Write Characters to a String
加密数据Encrypt data 加密数据Encrypting Data
解密数据Decrypt data 解密数据Decrypting Data

文件访问权限和属性File Access and Attributes

可以控制文件创建、打开,以及与 FileAccessFileModeFileShare 枚举分享的方式,枚举中包含 FileStream 类的构造函数使用的标志。You can control how files are created, opened, and shared with the FileAccess, FileMode, and FileShare enumerations, which contain the flags used by the constructors of the FileStream class. 例如,打开或新建 FileStream 时,FileMode 枚举允许指定是否打开文件供追加用、如果指定的文件不存在是否创建新文件、是否覆盖文件等。For example, when you open or create a new FileStream, the FileMode enumeration allows you to specify whether the file is opened for appending, whether a new file is created if the specified file does not exist, whether the file is overwritten, and so forth.

FileAttributes 枚举可启用特定于文件的信息收集。The FileAttributes enumeration enables the gathering of file-specific information. FileAttributes 枚举返回文件的存储属性,如是否压缩、加密、隐藏、只读、是否为存档、目录、系统文件或临时文件。The FileAttributes enumeration returns the file's stored attributes, such as whether it is compressed, encrypted, hidden, read-only, an archive, a directory, a system file, or a temporary file.

下表列出涉及文件访问和文件特性的任务:The following table lists tasks involving file access and file attributes:

To 请参阅See
打开并追加文本到日志文件Open and append text to a log file 如何:打开并追加到日志文件How to: Open and Append to a Log File
确定文件特性Determine the attributes of a file FileAttributes

文件权限File Permissions

可使用 FileIOPermission 类控制对文件和目录的访问权限。Controlling access to files and directories can be done with the FileIOPermission class. 这对使用 Web 窗体的开发人员来说尤其重要,这些窗体默认在名为 ASPNET 的特殊本地用户帐户上下文中运行,该帐户作为 ASP.NET 和 .NET Framework 安装的一部分创建。This may be particularly important for developers working with Web Forms, which by default run within the context of a special local user account named ASPNET, which is created as part of the ASP.NET and .NET Framework installations. 此类应用程序请求对资源的访问权限时,ASPNET 用户帐户的权限有限,这可能会阻止用户执行从 Web 应用程序写入到文件等操作。When such an application requests access to a resource, the ASPNET user account has limited permissions, which may prevent the user from performing actions such as writing to a file from a Web application. 有关详细信息,请参阅 FileIOPermissionFor more information, see FileIOPermission.

独立的文件存储Isolated File Storage

独立存储是一种尝试,为解决在用户或代码可能缺少必要权限的情况下处理文件时所产生的问题。Isolated storage is an attempt to solve problems created when working with files where the user or code may lack necessary permissions. 独立存储向每个用户分配数据隔离舱,它包含一个或多个存储区。Isolated storage assigns each user a data compartment, which can hold one or more stores. 可以按用户和程序集使存储区彼此独立。Stores can be isolated from each other by user and by assembly. 只有创建存储区的用户和程序集才有权访问存储区。Only the user and assembly that created a store have access to it. 存储区充当完整的虚拟文件系统,可以在一个存储区中创建并操作目录和文件。A store acts as a complete virtual file system—within one store you can create and manipulate directories and files.

下表列出了通常与独立文件存储相关联的任务。The following table lists tasks commonly associated with isolated file storage.

To 请参阅See
创建独立存储区Create an isolated store 如何:获取独立存储的存储区How to: Obtain Stores for Isolated Storage
枚举独立存储区Enumerate isolated stores 如何:枚举独立存储的存储区How to: Enumerate Stores for Isolated Storage
删除独立存储区Delete an isolated store 如何:删除独立存储中的存储区How to: Delete Stores in Isolated Storage
在独立存储中创建文件或目录Create a file or directory in isolated storage 如何:在独立存储中创建文件和目录How to: Create Files and Directories in Isolated Storage
在独立存储中查找文件Find a file in isolated storage 如何:在独立存储中查找现有文件和目录How to: Find Existing Files and Directories in Isolated Storage
在独立存储中从文件读取文件或向文件写入文件Read from or write to a file in isolated storage 如何:在独立存储中读取和写入文件How to: Read and Write to Files in Isolated Storage
在独立存储中删除文件或目录Delete a file or directory in isolated storage 如何:在独立存储中删除文件和目录How to: Delete Files and Directories in Isolated Storage

文件事件File Events

FileSystemWatcher 组件允许监视系统上或任何你对其具有网络访问权限的计算机上的文件和目录所作的更改。The FileSystemWatcher component allows you to watch for changes in files and directories on your system or on any computer to which you have network access. 例如,如果修改了文件,可能想要向用户发送更改警报。For example, if a file is modified, you might want to send a user an alert that the change has taken place. 发生更改时,会引发一个或多个事件并将其存储在缓冲区中,然后移交给 FileSystemWatcher 组件进行处理。When changes occur, one or more events are raised, stored in a buffer, and handed to the FileSystemWatcher component for processing.

另请参阅See also