Jeff Adkins' WebLog

Sr. Software Design Engineer, Microsoft

CSharp Bits:System.IO Namespace

Namespaces in the .NET Framework

The .NET Framework provides common language services to a variety of application development tools. The classes in the framework provide an interface to the common language runtime, the operating system, and the network.

System.IO Namespace

The System.IO namespace is important because it contains many classes that allow an application to perform input and output (I/O) operations in various ways through the file system.

The System.IO namespace also provides classes that allow an application to perform input and output operations on files and directories.

The System.IO namespace is large and cannot be explained in detail here. However, the following list gives an indication of the facilities available:

·           The File and Directory classes allow an application to create, delete, and manipulate directories and files.

·           The StreamReader and StreamWriter classes enable a program to access file contents as a stream of bytes or characters.

·           The FileStream class can be used to provide random access to files.

·           The BinaryReader and BinaryWriter classes provide a way to read and write primitive data types as binary values.

System.IO Example

The following example shows how a file can be opened and read as a stream. The example is not meant to illustrate all of the possible ways in which the System.IO namespace can be used, but does show how you can perform a simple file copy operation.

using System;
using System.IO; // Use IO namespace
// ...
StreamReader reader = new StreamReader("infile.txt");
// Text in from file
StreamWriter writer = new StreamWriter("outfile.txt");
// Text out to file
string line;
while ((line = reader.ReadLine( )) != null)
{
   writer.WriteLine(line);
}

reader.Close( );
writer.Close( );

To open a file for reading, the code in the example creates a new StreamReader object and passes the name of the file that needs to be opened in the constructor. Similarly, to open a file for writing, the example creates a new StreamWriter object and passes the output file name in its constructor. In the example, the file names are hard-coded, but they could also be string variables.

The example program copies a file by reading one line at a time from the input stream and writing that line to the output stream.

ReadLine and WriteLine might look familiar. The Console class has two static methods of that name. In the example, the methods are instance methods of the StreamReader and StreamWriter classes, respectively.

For more information about the System.IO namespace, search for "System.IO namespace" in the .NET Framework SDK Help documents.

Published Wednesday, February 18, 2004 1:29 PM by Jeff Adkins
Filed under:

Comments

 

Oompa Loompa said:

Your example code is VERY dangerous. You run the risk of leaving open file descriptors. That takes up resources, causes problems when shutting down applications, and prevents files from being accessed after a problem has occurred.

Your code should look more like the following:

StreamReader Reader = new StreamReader("infile.txt");
try
{
// Do operations here
} finally {
Reader.Close();
}

Do the same for the Writer as well.
February 18, 2004 2:22 PM
 

Joe Coder said:

What is the point of this anyway? C# has been out for a few years now, I think people are past the point of doing simple IO...
February 18, 2004 2:29 PM
 

Rj said:

Oompa - Thanks for the tip.

Joe - This is pretty useful, I just started C# and this got me thinking about a project I can upgrade from VB. Of course I could have found this anywhere but it gave me the seed of an idea and now has me actively looking for more
February 18, 2004 2:43 PM
 

Jeff Adkins said:

Oompa - Your point is very salient. The point, however, of the example is to demonstrate the topic. It is not shippable code by any stretch of the imagination. CSharp Bits is a tutorial-based, one topic per day, digest explaining C# concepts in general. It evolves, covering prerequisite topics and is currently broad in coverage rather than getting deep. That will come after a basis has been established. Hang on for the ride.
February 18, 2004 6:47 PM
 

TrackBack said:

CSharp Bits:System.IO Namespace
February 19, 2004 10:26 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker