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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …