![]() |
VOOZH | about |
In this article, we will explain the FileInfo class, its methods, properties, etc. The System.IO namespace is one of the most important namespaces we used while we are working with files in C#.
It does not have static methods and it can only be used on instantiated objects. The class objects represent a file on a disk. The FileInfo class also provides instance methods for the creation, copying, deletion, moving, and opening of files.
| Methods | Description |
|---|---|
| Create | This method is used when we have to create a new file. |
| CreateText | This method creates a stream writer whose work is to write a new text file. |
| Delete | This method is used when we have to delete an already existing file. |
| CopyTo | This method is used to copy the existing file into a new file. |
| MoveTo | This method is used when we have to move a file from one place to another place. |
| AppendText | This method creates a stream writer whose work is to append the text to the file represented by the instance of FileInfo class. |
| OpenText | This method creates a StreamReader which has a UTF8 encoding whose work is to read from an already existing text file. |
The FileInfo class provides the following properties:
| Property | Description |
|---|---|
| CreationTime | Its work is to return the creation of the file date and time. |
| Exists | This property checks for the presence of a file before working on it. |
| Extension | This property returns the type of file. |
| FullName | This property returns the full name of the file. |
| Name | This property returns the name of the file. |
| LastWriteTime | This property returns the date and time of the last file saved. |
| Length | This property retrieves the size of a file. |
Below is the C# program which illustrates the methods of FileInfo class. Now, here we will use the FileInfo class to perform the following operations which are: copying, moving, renaming, creating, opening, deleting, and appending to files.
Example 1:
Output: