We will see how to work with files and directories
C# uses a network of related namespaces and classes to support a file system, allowing you to read and write data and create, delete, and move files. These are the
· System.IO namespace
· Path class
· File and Directory classes
· FileSystemInfo class
· FileInfo and DirectoryInfo classes
The System.IO namespace is the overall namespace that deals with the input and output of file information. It contains the classes and objects used in file manipulation, as well as the related delegates, exception classes, and structures.
The Path class contains several static methods used to handle pathnames. These methods don't perform any new operations but make it easier to handle pathnames.
Path.Combine is used to find the path to a file, and is the most common Path method, but the class also contains methods for supplying information about a path.
The main function of the Path class is to make methods available. You do not need to create instances of the class.
The Path class is already aware of different operating systems and the different pathname formats they use, so it is especially useful if .NET is ported to other operating systems.
The Directory and File classes are derived from the Object class but are never instantiated. They contain static methods, which are used by supplying the path to the file or directory on which the method will operate.
Methods contained by the File and Directory classes include
· Create ( )
· Delete ( )
· Move ( )
· Copy ( )
· GetDirectories ( )
· GetFiles ( )
Create ( )
The Create method allows you to create a new file or folder with the name you specify.
Delete ( )
The Delete method allows you to delete a file or folder. You can also choose to automatically delete all of the files contained in a folder.
Move ( )
The Move method moves a file or folder and can also be used to change the object's name.
Copy ( )
The Copy method is part of the File class and is used to create a copy of the file. There is no equivalent method in the Directory class.
GetDirectories ( )
The GetDirectories method of the DirectoryInfo class examines a folder and returns an array containing an object for each subfolder in the original folder.
GetFiles ( )
The GetFiles method of the DirectoryInfo class returns an array of objects representing the files in a folder.
FileSystemInfo is an abstract base class that represents any object in the file system. The FileInfo and DirectoryInfo classes are members of the FileSystemInfo class.
Like the File and Directory classes, the FileInfo and DirectoryInfo classes contain methods used to manipulate file-system objects. However, the FileInfo and DirectoryInfo classes contain all instance methods.
So when the FileInfo and DirectoryInfo classes are used, you create instances of the FileInfo and DirectoryInfo classes and use the methods to operate on them.
FileInfo ThisFile = new
This makes the FileInfo and DirectoryInfo classes more efficient when multiple operations are performed on the same object. For example, the FileInfo object that is created stores information about the file being handled, so this information doesn't have to be retrieved every time the file is called.
To instantiate an object from the FileInfo and DirectoryInfo classes, you need the path to the file or directory.
The path must lead to an object of the correct type. If the type is incorrect, or the path doesn't lead to anything, an exception is thrown when the object is called.
If you are simply trying to discover information about an object, and that information is available, it may not matter if you specify the wrong type of object. For example, if you try to find out when a file was created, but the path you supply leads to a directory, the creation time of the directory is returned.
The FileInfo and DirectoryInfo classes contain many of the same methods as the File and Directory classes.
For example, you use the MoveTo method of the FileInfo class to move a file to another destination.
FileInfo This File = new FileInfo(ThePathName);
And you use the Move method of the File class to move a file to another destination.
Both of these achieve the same thing, but the Move method of the File class requires an extra parameter.
The System.Console class handles input for simple console applications. It includes three properties:
The Read method of Console.In reads the next single character of input from the keyboard or from a file. The ReadLine method reads the next line of input.
The console output can be directed to the screen or to a file. The Write and WriteLine methods can output different types of data – the only difference being that WriteLine starts a new line of output.
The Console.Error property gets the standard error output stream. It is set to the standard error stream by default. This property can be set to another stream with the SetError method.
Thanks and Have Fun!!!!!