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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More