共用方式為


Common I/O Tasks 

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. For more information, see File and Stream I/O.

Common File Tasks

To do this... See the example in this topic...

Create a text file.

System.IO.File

Write to a text file.

How to: Write Text to a File

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

System.IO.File.AppendText(System.String)

System.IO.FileInfo.AppendText

Rename or move a file.

System.IO.File.Move(System.String,System.String)

System.IO.FileInfo.MoveTo(System.String)

Delete a file.

System.IO.File.Delete(System.String)

System.IO.FileInfo.Delete

Copy a file.

System.IO.File.Copy(System.String,System.String)

System.IO.FileInfo.CopyTo(System.String)

Get the size of a file.

System.IO.FileInfo.Length

Get the attributes of a file.

System.IO.File.GetAttributes(System.String)

Set the attributes of a file.

System.IO.File.SetAttributes(System.String,System.IO.FileAttributes)

Determine whether a file exists.

System.IO.File.Exists(System.String)

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 extension.

System.IO.Path.GetExtension(System.String)

Retrieve the fully qualified path of a file.

System.IO.Path.GetFullPath(System.String)

Retrieve the file name and extension from a path.

System.IO.Path.GetFileName(System.String)

Change the extension of a file.

System.IO.Path.ChangeExtension(System.String,System.String)

Common Directory Tasks


To do this... See the example in this topic...

Rename or move a directory.

System.IO.Directory.Move(System.String,System.String)

System.IO.DirectoryInfo.MoveTo(System.String)

Delete a directory.

System.IO.Directory.Delete(System.String)

System.IO.DirectoryInfo.Delete

Create a directory.

System.IO.Directory.CreateDirectory(System.String)

System.IO.FileInfo.Directory

Create a subdirectory.

System.IO.DirectoryInfo.CreateSubdirectory(System.String)

See the files in a directory.

System.IO.FileInfo.Name

See the subdirectories of a directory.

System.IO.Directory.GetDirectories(System.String)

System.IO.DirectoryInfo.GetDirectories

See all the files in all subdirectories of a directory.

System.IO.DirectoryInfo.GetFileSystemInfos(System.String)

Find the size of a directory.

System.IO.Directory

Determine whether a directory exists.

System.IO.Directory.Exists(System.String)

See Also

Concepts

Basic File I/O
Composing Streams
Asynchronous File I/O

Other Resources

File and Stream I/O