VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-fileinfo-class-methods/

⇱ C# - FileInfo Class Methods - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# - FileInfo Class Methods

Last Updated : 9 Dec, 2022

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#.

FileInfo Class:

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 of FileInfo Class:

MethodsDescription
 CreateThis method is used when we have to create a new file.
CreateTextThis 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.
CopyToThis method is used to copy the existing file into a new file. 
MoveToThis method is used when we have to move a file from one place to another place.
AppendTextThis method creates a stream writer whose work is to append the text to the file represented by the instance of FileInfo class.
OpenTextThis method creates a StreamReader which has a UTF8 encoding whose work is to read from an already existing text file.

Properties of FileInfo Class:

The FileInfo class provides the following properties:

PropertyDescription
CreationTimeIts work is to return the creation of the file date and time.
ExistsThis property checks for the presence of a file before working on it.
ExtensionThis property returns the type of file.
FullNameThis property returns the full name of the file.
NameThis property returns the name of the file.
LastWriteTimeThis property returns the date and time of the last file saved.
LengthThis 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:

👁 Image
 
Comment
Article Tags:

Explore