Dela via


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

File.AppendText

FileInfo.AppendText

Rename or move a file.

File.Move

FileInfo.MoveTo

Delete a file.

File.Delete

FileInfo.Delete

Copy a file.

File.Copy

FileInfo.CopyTo

Get the size of a file.

FileInfo.Length

Get the attributes of a file.

File.GetAttributes

Set the attributes of a file.

File.SetAttributes

Determine whether a file exists.

File.Exists

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.

Path.GetExtension

Retrieve the fully qualified path of a file.

Path.GetFullPath

Retrieve the file name and extension from a path.

Path.GetFileName

Change the extension of a file.

Path.ChangeExtension

Common Directory Tasks

To do this...

See the example in this topic...

Rename or move a directory.

Directory.Move

DirectoryInfo.MoveTo

Copy a Directory

How to: Copy Directories

Delete a directory.

Directory.Delete

DirectoryInfo.Delete

Create a directory.

Directory.CreateDirectory

FileInfo.Directory

Create a subdirectory.

DirectoryInfo.CreateSubdirectory

See the files in a directory.

FileInfo.Name

See the subdirectories of a directory.

Directory.GetDirectories

DirectoryInfo.GetDirectories

See all the files in all subdirectories of a directory.

DirectoryInfo.GetFileSystemInfos

Find the size of a directory.

System.IO.Directory

Determine whether a directory exists.

Directory.Exists

See Also

Concepts

Basic File I/O

Composing Streams

Asynchronous File I/O

Other Resources

File and Stream I/O