File I/O functionality in C# depends largely on the underlying BCL. There are no keywords in C# for dealing with file I/O specifically. [1] This chapter will familiarize you with some of the common file I/O operations so that you can start using them immediately.
[1] This is the same case for Java too. File I/O depends wholly on the underlying Java core classes rather than the language itself.
The classes will not be described fully, and there is usually more than one way to accomplish something. You will be expected to refer to the API documentation for more information.
In order to execute the codes in this chapter, you need the necessary permissions to manipulate your file system. [2]
[2] Permissions and security will not be discussed here, but if you are experimenting on your local Windows machine and you have administrator privileges, you should be able to run the codes without any security exceptions.
I shall discuss how to manipulate files and directories [3] using the Directory, File, DirectoryInfo, and FileInfo classes. This is followed by sections on how to read/write from text files and binary files.
[3] Directories are also known as 'folders' in Windows nomenclature. However, Microsoft has selected to use 'directory' instead of folder for naming purposes in the .NET class libraries.
Figure 18.1 lists the classes that are important in file I/O. Table 18.1 lists the classes involved in I/O operations and Table 18.2 gives some exception classes.
Class | Description |
---|---|
FileInfo | Represents a file |
File | Represents a file – contains only static methods, and cannot be instantiated |
DirectoryInfo | Represents a directory – exposes instance methods for creating and moving through directories and subdirectories |
Directory | Represents a directory – contains only static methods for creating and moving through directories and subdirectories; this class cannot be instantiated |
Path | Contains static members for manipulating path names |
StreamReader | Implements a TextReader that reads characters from a byte stream |
StreamWriter | Implements a TextWriter that writes characters to a byte stream |
StringReader | Implements a TextReader that reads from a string |
StringWriter | Writes to a string |
TextReader | A reader that reads a sequential series of characters |
TextWriter | A writer that writes a sequential series of characters |
FileStream | Exposes a Stream object around a file for synchronous and asynchronous read/write operations |
Exception class | Description |
---|---|
FileNotFoundException | Thrown when a file cannot be found |
DirectoryNotFoundException | Thrown when part of a file or directory cannot be found |
EndOfStreamException | Thrown when an attempt is made to read past end of a stream |
IOException | Thrown when an general I/O error occurs – for example, when an attempt is made to copy a file to a target path which already contains a file of the same name |
PathTooLongException | Thrown when a path or file name is longer than the maximum length defined by the system |
FileLoadException | Thrown when file is found but cannot be loaded |
18.117.232.191