oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

C# Input/Output Classes Simplified
Pages: 1, 2, 3, 4, 5


Moves a file to another directory. The Move method has the same syntax as the first overload of the Copy method. The difference from the Copy method is that Move deletes the source file.


Opens a file and returns a FileStream object. If the file to be opened does not exist, a FileNotFoundException exception is thrown. This method has three overloads, all of which require that you pass a path and filename as the first argument and a member of the FileMode enumeration as the second. The second and third overloads require a member of the FileAccess enumeration as their third argument, while the third overload requires a member of the FileShare enumeration as its fourth argument.

The FileMode enumeration tells the operating system how the file should be opened. Its members are Append, Create, CreateNew, Open, OpenOrCreate, and Truncate.

Related Reading

Programming C#
By Jesse Liberty

The FileAccess enumeration indicates to the operating system whether the file is to be opened for reading or writing. Its members are Read, ReadWrite, and Write. The FileShare enumeration tells the operating system how to control access to the same opened file from other threads, processes, or users. Its members are Inheritable, None, Read, ReadWrite, and Write. If the value of the argument is FileShare.Write, for example, other programs can write to the opened file.


Opens a file as read-only and returns a FileStream object. Its only parameter is the path and name of the file. This is a single-purpose version of the Open method.


Opens a file and returns a StreamReader object. Its only parameter is the path and name of the file.


Opens a file and returns a Stream object. You can use this Stream object to read and write to the file. The method's only parameter is the path and name of the file.


Obtains the file's attributes, as expressed in a bitwise combination of the members of the FileAttributes enumeration: Archive, Compressed, Device, Directory, Encrypted, Hidden, Normal, NotContentIndexed, Offline, ReadOnly, ReparsePoint, SparseFile, System, and Temporary.


Changes the attribute of a file. You pass a file path and a bitwise combination of the members of the FileAttribute enumeration to this method.

The FileInfo Class

The File class has static methods that can be conveniently used without having to have an instance of a File object; however, those methods always perform security checks that adversely affect performance. The FileInfo class provides similar methods for similar tasks, but without security checks. You should use FileInfo if you are going to access a file several times. Also, unlike File, methods in FileInfo are instance methods. To create a FileInfo object, pass a valid file path to its constructor. The Create method, for example, behaves like the Create method of the File class. For instance, the following code uses the FileInfo class to create a file:

FileInfo fileInfo = new FileInfo("C:/NextGen.txt");
FileStream fs = fileInfo.Create();

The Directory Class

The Directory class represents a directory. Its constructor is private, so you cannot use the new keyword to construct a Directory object. That is not necessary, in any case, because all methods of the Directory class are static. The Directory class performs security checks on all of its methods, resulting in slightly reduced performance. If you will be accessing the same directory often, use the DirectoryInfo class instead; it is described in the section "The DirectoryInfo Class."

The following are some of the important methods of the Directory class:


Creates a directory in the specified path. If the directory's parent does not exist, the parent directory will also be created. For example, if you pass the path C:\data\projects\RD to this method and the projects directory does not exist, both the RD and projects directories will be created. The return value of this method is a DirectoryInfo object representing the created directory. For example:

DirectoryInfo dirInfo = Directory.CreateDirectory("
System.Console.WriteLine(dirInfo.Name); // prints "RD"

For more information about DirectoryInfo, see the "The DirectoryInfo Class" section.

Pages: 1, 2, 3, 4, 5

Next Pagearrow