C# Input/Output Classes Simplified
by Budi Kurniawan08/05/2002
Beginners to the .NET Framework sometimes have a hard time trying to understand the
classes in the System.IO namespace for performing input/output (IO) operations. The
difficulty stems from the fact that this namespace is relatively large, containing more
than 40 members, some of which are similar classes that can be used to achieve the same
tasks. Thus it is sometimes hard to figure out which class is best for which task. This
article tries to make your life a bit easier by grouping IO tasks into three categories
and introducing the classes that are suitable for each task category.
Input/output (IO) refers to the operations for reading and writing data to streams and
files. In the .NET Framework, you use the classes in the System.IO namespace to
perform these operations. This article starts with the basic classes such as the File and FileInfo classes -- classes used to perform file operations such as creating, copying, and
deleting files. Other basic classes include Directory and DirectoryInfo, which provide
functionality to manipulate directories, and Path, which supports application portability.
In addition to those classes, there are also classes you can use to read and write the
contents of a file. The System.IO namespace provides a number of classes
that allow you to do basically similar things. These classes can be roughly
grouped into three categories:
|
Related Reading
C# in a Nutshell |
- Classes for reading and writing bytes.
- Classes for reading and writing characters.
- Classes for reading and writing binary data.
In the first group are stream classes that are derived from the Stream class. In the second
category are reader and writer classes, such as StreamReader and StreamBuffer. The
third group includes classes such as BinaryReader and BinaryWriter. Which class you
should use for your input and output operations will depend on the kind of data you are
going to work with.
The File Class
You use the File class to manipulate a file; to create, copy, or delete it, and check if it exists. You also use it to open a file for reading and writing. To read from and write to
a file itself, however, you need to use other classes, such as the FileStream or the
BufferedStream class.
The File class constructor is private, so you cannot instantiate a file using the new
keyword. This is not necessary because all methods of the File class are static. These
static methods perform security checks every time they are called. This, of course, has
performance consequences, and sometimes is not needed. If you need to access the same
file frequently, use the FileInfo class instead.
The following is the list of the most important methods of the File class:
Create
Creates a file in a specified directory. You call this method passing a file path or a file path and an
integer of a buffer size for the resulting FileStream object. For example, the following code creates a file
called newFile.txt in the C:\data directory, returns a FileStream, and then closes the FileStream
object straight away:
FileStream fs = File.Create("C:/data/newFile.txt");
fs.Close();
For more information on the FileStream class, see the section "The FileStream Class" later in this
article. As another example, the following code creates a FileStream with a buffer size of 16KB.
FileStream fs = File.Create("
C:/123data/newFile.txt", 16384);
If the file c:\data\newfile.txt already exists, it will be overwritten.
CreateText
Creates a file and returns a StreamWriter object that you can use to write text into that file. Like Create,
you pass a file path to the method, and any existing file will be overwritten. For example:
StreamWriter sw = File.CreateText("
C:/123data/new.txt");
See the "The StreamWriter Class" section later in this article for more details on StreamWriter.
AppendText
Returns a StreamWriter object to a specified existing file. The text you write will then be appended to the end of the file. If the file does not exist, AppendText will create a new file. You pass a file path to
this method.
Exists
Returns true if a file in a specified path exists; otherwise, it returns false. Its only parameter is a path and filename.
Copy
Copies a file to another folder. There are two overloads of this method, as follows:
public static void Copy(
string sourceFileName,
string destFileName
);
public static void Copy(
string sourceFileName,
string destFileName,
bool overwrite
);
The first overload of the method will throw an IOException if the destination file already
exists. So will the second overload, if the overwrite argument is false. For both overloads, a
FileNotFoundException will be thrown if the source file does not exist. In addition, the
method can throw an ArgumentException if either sourceFilename or
destinationFilename is invalid.

