Opening and Closing a File

To create a byte stream linked to a file, create a FileStream object. FileStream defines several constructors. Perhaps its most commonly used one is shown here:
FileStream(string path, FileMode mode)
Here, path specifies the name of the file to open, which can include a full path specification. The mode parameter specifies how the file will be opened. It must be one of the values defined by the FileMode enumeration. These values are shown in Following Table. In general, this constructor opens a file for read/write access. The exception is when the file is opened using FileMode.Append. In this case, the file is write-only.

Value Description
FileMode.Append Output is appended to the end of file.
FileMode.Create Creates a new output file. Any preexisting file by the same name will be destroyed.
FileMode.CreateNew Creates a new output file. The file must not already exist.
FileMode.Open Opens a preexisting file.
FileMode.OpenOrCreate Opens a file if it exists, or creates the file if it does not already exist.
FileMode.Truncate Opens a preexisting file, but reduces its length to zero.

There are many ways to handle the process of opening a file. The following shows one way. It opens a file called test.dat for input.

FileStream fin = null;
try
{
    fin = new FileStream("test", FileMode.Open);
}
catch (IOException exc)
{ // catch all I/O exceptions
    Console.WriteLine(exc.Message);
    // Handle the error.
}
catch (Exception exc)
{ // catch any other exception
    Console.WriteLine(exc.Message);
    // Handle the error, if possible.
    // Rethrow those exceptions that you don't handle.
}

Here, the first catch clause handles situations in which the file is not found, the path is too long, the directory does not exist, or other I/O errors occur. The second catch, which is a “catch all” clause for all other types of exceptions, handles the other possible errors (possibly by rethrowing the exception). You could also check for each exception individually, reporting more specifically the problem that occurred and taking remedial action specific to that error.
 
The FileStream constructor just described opens a file that (in most cases) has read/write access. If you want to restrict access to just reading or just writing, use this constructor instead:
FileStream(string path, FileMode mode, FileAccess access)
As before, path specifies the name of the file to open, and mode specifies how the file will be opened. The value passed in access specifies how the file can be accessed. It must be one of the values defined by the FileAccess enumeration, which are shown here:
FileAccess.Read
FileAccess.Write
FileAccess.ReadWrite

For example, this opens a read-only file:

FileStream fin = new FileStream("test.dat", FileMode.Open, FileAccess.Read);

When you are done with a file, you must close it. This can be done by calling Close( ). Its general form is shown here:
void Close( )
Closing a file releases the system resources allocated to the file, allowing them to be used by another file. As a point of interest, Close( ) works by calling Dispose( ), which actually frees the resources.