通用 I/O 任务Common I/O Tasks

System.IO 命名空间提供若干个类,通过这些类可以对文件、目录和流执行各种操作(如读取和写入)。The System.IO namespace provides several classes that allow for various actions, such as reading and writing, to be performed on files, directories, and streams. 有关详细信息,请参阅文件和流 I/OFor more information, see File and Stream I/O.

通用文件任务Common File Tasks

若要执行此操作...To do this... 请参见本主题中的示例...See the example in this topic...
创建文本文件Create a text file File.CreateText 方法File.CreateText method

FileInfo.CreateText 方法FileInfo.CreateText method

File.Create 方法File.Create method

FileInfo.Create 方法FileInfo.Create method
写入到文本文件Write to a text file 如何:将文本写入文件How to: Write Text to a File

如何:编写文本文件 (C++/CLI)How to: Write a Text File (C++/CLI)
从文本文件读取Read from a text file 如何:从文件中读取文本How to: Read Text from a File
向文件中追加文本Append text to a file 如何:打开并追加到日志文件How to: Open and Append to a Log File

File.AppendText 方法File.AppendText method

FileInfo.AppendText 方法FileInfo.AppendText method
重命名或移动文件Rename or move a file File.Move 方法File.Move method

FileInfo.MoveTo 方法FileInfo.MoveTo method
删除文件Delete a file File.Delete 方法File.Delete method

FileInfo.Delete 方法FileInfo.Delete method
复制文件Copy a file File.Copy 方法File.Copy method

FileInfo.CopyTo 方法FileInfo.CopyTo method
获取文件大小Get the size of a file FileInfo.Length 属性FileInfo.Length property
获取文件特性Get the attributes of a file File.GetAttributes 方法File.GetAttributes method
设置文件特性Set the attributes of a file File.SetAttributes 方法File.SetAttributes method
确定文件是否存在Determine whether a file exists File.Exists 方法File.Exists method
从二进制文件读取Read from a binary file 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File
写入二进制文件Write to a binary file 如何:对新建的数据文件进行读取和写入How to: Read and Write to a Newly Created Data File
检索文件扩展名Retrieve a file name extension Path.GetExtension 方法Path.GetExtension method
检索文件的完全限定路径Retrieve the fully qualified path of a file Path.GetFullPath 方法Path.GetFullPath method
检索路径中的文件名和扩展名Retrieve the file name and extension from a path Path.GetFileName 方法Path.GetFileName method
更改文件扩展名Change the extension of a file Path.ChangeExtension 方法Path.ChangeExtension method

通用目录任务Common Directory Tasks

若要执行此操作...To do this... 请参见本主题中的示例...See the example in this topic...
访问特定文件夹(如“My Documents”)中的文件Access a file in a special folder such as My Documents 如何:将文本写入文件How to: Write Text to a File
创建目录Create a directory Directory.CreateDirectory 方法Directory.CreateDirectory method

FileInfo.Directory 属性FileInfo.Directory property
创建子目录Create a subdirectory DirectoryInfo.CreateSubdirectory 方法DirectoryInfo.CreateSubdirectory method
重命名或移动目录Rename or move a directory Directory.Move 方法Directory.Move method

DirectoryInfo.MoveTo 方法DirectoryInfo.MoveTo method
复制目录Copy a directory 如何:复制目录How to: Copy Directories
删除目录Delete a directory Directory.Delete 方法Directory.Delete method

DirectoryInfo.Delete 方法DirectoryInfo.Delete method
查看目录中的文件和子目录See the files and subdirectories in a directory 如何:枚举目录和文件How to: Enumerate Directories and Files
查明目录大小Find the size of a directory System.IO.DirectorySystem.IO.Directory class
确定目录是否存在Determine whether a directory exists Directory.Exists 方法Directory.Exists method

请参阅See also