C# FileInfo Class4 min read

In C#, FileInfo class provides properties and instance methods that are used to deal with the various operations in a file such as creating, copying, deleting, moving, etc.

It is derived from FileSystemInfo class and is a part of System.IO namespace.

C# FileInfo Properties

The following are some of the most used properties in FileInfo class.

PropertyDescription
DirectoryIt returns an instance of the parent directory of a file.
DirectoryNameIt retrieves the directory full path as a string.
ExistsIt returns a value that indicates whether the file exists or not.
IsReadOnlyIt is used to get or set a value that determines whether the current file is read-only or not.
LengthThis property gets the size of the current file in bytes.
NameIt is used to get the name of the file.
ExtensionThis will return an extension part of the file.
CreationTimeIt is used to get or set the creation time of the current file or directory.
LastAccessTimeIt is used to get or set the last access time of the current file or directory.
LastWriteTimeIt is used to get or set the time when the current file or directory was last written to.

C# FileInfo Methods

The table below lists the methods used in FileInfo class.

MethodDescription
Create()This method is used to create a file.
CopyTo(String)This method copies an existing file to a new file, but it won’t allow overwriting an existing file.
CopyTo(String, Boolean)This method will copy an existing file to a new file, allowing overwriting an existing file.
CreateText()It creates a StreamWriter that writes a new text file.
AppendText()It creates a StreamWriter that appends text to the file.
Encrypt()This method is useful to encrypt a file so that only the account used to encrypt the file can decrypt it.
Decrypt()This method is useful to decrypt a file that is encrypted by the current account using the encrypt method.
Delete()It deletes the file permanently.
Open()This method opens a file in the specified mode.
OpenRead()This method creates a read-only file stream.
OpenWrite()This method creates a write-only file stream.
OpenText()This method creates a stream reader that reads from an existing text file.
Replace()This method is used to replace the contents of the specified file with the file described by the current fileinfo object.
ToString()It returns the path as a string.

Now, let us go through an example in C# for FileInfo.


Example: C# FileInfo program for creating a file

Output: After execution, you will see the following result.

File is created Successfuly

Also, since we created a new file, a file name “sample.text” will be created in the same path where you have saved your code file. Although, if you want to change the directory of the newly created file then you can give the full pathname instead of just the file name.


Example: C# FileInfo program to Write and Read from a file

Output: After execution, you will see the following result.

Hi! Welcome to Simple2code.com.

A file will be created and the above text is written on it. The program first writes the string on a text file and then reads from it.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More