File Input and Output

A file is aggregated data stored to disk, and it becomes either an input (read) or output(write) stream when opened for processing.

File input and output uses the namespace System.IO, which contains a number of important classes in file operations. Review a selection of classes below:

Name Description
BinaryReader It performs a read of primitive data in a binary stream.
BufferedStream It temporarily stores a stream of bytes.
Directory It aids in directory structure manipulation.
DirectoryInfo It operates on directories.
DriveInfo It delivers information about the drives.
File It manipulates files.
FileInfo It performs operations on files.
FileStream It reads and writes to a location in a file.
MemoryStream It is employed for random access to a data stream stored in memory.
Path It performs operations on path information.
StreamReader It reads characters from a byte stream.
StreamWriter It writes characters to a stream.
StringReader It reads from a string buffer.
StringWriter It writes to a string buffer.

THE STREAM CLASS

The Stream class manages read and write manipulation. The StreamReader and StreamWriter classes specifically manage these areas. StreamReader inherits from TextReader, and represents a reader tool for reading a set of characters. Its methods follow: Close, closes the object and releases associated resources; Peek, returns the next character without consuming; Read, reads the next character; ReadLine, reads a line and returns a string; and Seek, moves the read/write position.

FILEMODE ENUMERATION

FileMode parameters specified in file operation constructors specify how a file will be opened. The parameters determine the action on the file: overwrite, creation, open, or a combination. Review its syntax below:

public enum FileMode

Its members include Append, Create, CreateNew, Open, OpenOrCreate, and Truncate.

MAKE A FILE

Produce a text file using any number of applications, or employ addListItem:

private void addListItem(string value)
{
	this.listBoxOne.Items.Add(value);
}

You can also use this statement:

this.listBoxOne.Items.Add(value);

ACCESSING A FILE

Use the StreamReader class to read a file. The contents of the file are then added to a ListBox control. Many ways exist for checking if the end of the file has been reached. One approach uses the Peek method. Review an example of reading from a file below (this code uses the winDir variable):

StreamReader reader=new StreamReader(winDir + "\\stuff.ini");
try
{
	do
	{
		addListItem(reader.ReadLine());
	}
	while(reader.Peek() != -1);
}
catch
{
	addListItem("File has no content");}
finally
{
	reader.Close();
}

MODIFYING A FILE

The StreamWriter class can be used to create and write to a file. It also provides a way to open an existing file using the same approach. Review an example of its use below:

StreamWriter writer = new StreamWriter("c:\\testfile.txt");
	writer.WriteLine("File spawned with the StreamWriter class.");
	writer.Close();
	this.listboxOne.Items.Clear();
	addListItem("Written to C:\\testfile.txt");

VIEW FILE DATA

The FileInfo object can be used to access file properties. In the example below, Notepad.exe is used to gather information, but virtually any editor can be used (this code uses the winDir variable).

FileInfo FileProps =new FileInfo(winDir + "\\notepad.exe");
	addListItem("Name of file = " + FileProps.FullName);
	addListItem("Time of creation = " + FileProps.CreationTime);
	addListItem("Time of last access = " + FileProps.LastAccessTime);
	addListItem("Time of last write = " + FileProps.LastWriteTime);
	addListItem("File size = " + FileProps.Length);
	FileProps = null;

ACCESS DISK DRIVES

The Directory and Drive classes can be used to list logical drives. In the example below, the ListBox control is used for results:

string[] drives = Directory.GetLogicalDrives();
	foreach(string drive in drives)
	{
		addListItem(drive);
}

The GetDirectories method and GetFiles method of the Directory class can be utilized to list folders and files.