Introduction
The FileInfo class in the .NET and C# provides functions to work with files. This code sample in this tutorial covers most of the functionality provided by the class, such as getting file properties such as file size, creation time, last updated, ready only, the file exists, and so on. The tutorial also covers how to create a file, read a file, move, replace, and delete a file using C# and . NET.
Create a FileInfo
A FileInfo object is created using the default constructor that takes a string as a file name with a full path.
string fileName = @"C:\Temp\MaheshTXFI.txt";
FileInfo fi = new FileInfo(fileName);
FileInfo Properties
The FileInfo class provides properties to get the file name, extension, directory, size, and file attributes.
Get File Name
The FileName property returns just the file name part of the full path of a file. The following code snippet returns the file name.
string justFileName = fi.Name;
Console.WriteLine("File Name: {0}", justFileName);
Get the Full Path of a File
The FullName property returns just the full path of a file, including the file name. The following code snippet returns the full course of a file.
string fullFileName = fi.FullName;
Console.WriteLine("File Name: {0}", fullFileName);
Get a File Extension
The Extension property returns the extension of a file. The following code snippet returns the extension of a file.
string extn = fi.Extension;
Console.WriteLine("File Extension: {0}", extn);
Get the Directory Name of a File
The DirectoryName property returns the name of the directory of a file. The following code snippet returns the directory of a file.
string directoryName = fi.DirectoryName;
Console.WriteLine("Directory Name: {0}", directoryName);
Check if a File Exists
The Exists property returns true if a file exists. The following code snippet returns true if a file already exists.
bool exists = fi.Exists;
Get a file size
The Length property returns the size of a file in bytes. The following code snippet returns the size of a file.
// Get file size
long size = fi.Length;
Console.WriteLine("File Size in Bytes: {0}", size);
Check if a file is read-only
The IsReadOnly property returns if a file is read-only. The following code snippet returns true if a file is read only.
// File ReadOnly ?
bool IsReadOnly = fi.IsReadOnly;
Console.WriteLine("Is ReadOnly: {0}", IsReadOnly);
File Creation Time
The CreationTime property returns the DateTime when a file is created. The following code snippet returns the creation time of a file.
// Creation, last access, and last write time
DateTime creationTime = fi.CreationTime;
Console.WriteLine("Creation time: {0}", creationTime);
File Last Access Time
The LastAccessTime property returns the DateTime when a file was last accessed. The following code snippet returns the last access time of a file.
DateTime accessTime = fi.LastAccessTime;
Console.WriteLine("Last access time: {0}", accessTime);
File Last Updated Time
The LastWriteTime property returns the DateTime when a file was last updated or written. The following code snippet returns the last write time of a file.
DateTime updatedTime = fi.LastWriteTime;
Console.WriteLine("Last write time: {0}", updatedTime);
Sample
Here is a complete sample.
// Full file name
string fileName = @"C:\Temp\MaheshTXFI.txt"; FileInfo fi = new FileInfo(fileName);
// Create a new file
using (FileStream fs = fi.Create())
{
Byte[] txt = new UTF8Encoding(true).GetBytes("New file.");
fs.Write(txt, 0, txt.Length);
Byte[] author = new UTF8Encoding(true).GetBytes("Author Mahesh Chand");
fs.Write(author, 0, author.Length);
}
// Get File Name
string justFileName = fi.Name; Console.WriteLine("File Name: {0}", justFileName);
// Get file name with full path
string fullFileName = fi.FullName; Console.WriteLine("File Name: {0}", fullFileName);
// Get file extension
string extn = fi.Extension; Console.WriteLine("File Extension: {0}", extn);
// Get directory name
string directoryName = fi.DirectoryName; Console.WriteLine("Directory Name: {0}", directoryName);
// File Exists ?
bool exists = fi.Exists;
Console.WriteLine("File Exists: {0}", exists);
if (fi.Exists)
{
// Get file size
long size = fi.Length;
Console.WriteLine("File Size in Bytes: {0}", size);
// File ReadOnly ?
bool IsReadOnly = fi.IsReadOnly; Console.WriteLine("Is ReadOnly: {0}", IsReadOnly);
// Creation, last access, and last write time
DateTime creationTime = fi.CreationTime;
Console.WriteLine("Creation time: {0}", creationTime);
DateTime accessTime = fi.LastAccessTime;
Console.WriteLine("Last access time: {0}", accessTime);
DateTime updatedTime = fi.LastWriteTime;
Console.WriteLine("Last write time: {0}", updatedTime);
}
Create a File
We can create a file in two different following methods.
- File. Create
- File.CreateText
FileInfo.Create Method
The FileInfo.Create method creates a file at the given path. If a file name is provided without a path, the file will be created in the current folder.
The following code snippet creates a file using the Create method that returns a FileSteam object. The Write method of FileStream can be used to write text to the file.
// Full file name
string fileName = @"C:\Temp\MaheshTXFI.txt";
FileInfo fi = new FileInfo(fileName);
try
{
// Check if the file already exists. If yes, delete it.
if (fi.Exists)
{
fi.Delete();
}
// Create a new file
using (FileStream fs = fi.Create())
{
Byte[] txt = new UTF8Encoding(true).GetBytes("New file.");
fs.Write(txt, 0, txt.Length);
Byte[] author = new UTF8Encoding(true).GetBytes("Author Mahesh Chand");
fs.Write(author, 0, author.Length);
}
// Write file contents on the console.
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
FileInfo.CreateText Method
The FileInfo.createText method creates and opens a file for writing UTF-8 encoded text. If the file already exists, this method opens the file.
The following code snippet creates a file using the CreateText method that returns a StreamWriter object. The WriteLine method of SteamLine can be used to add line text to the object and writes to the file.
// Full file name
string fileName = @"C:\Temp\MaheshTXFITx.txt";
FileInfo fi = new FileInfo(fileName);
try
{
// Check if the file already exists. If yes, delete it.
if (fi.Exists)
{
fi.Delete();
}
// Create a new file
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine("New file created: {0}", DateTime.Now.ToString());
sw.WriteLine("Author: Mahesh Chand");
sw.WriteLine("Add one more line ");
sw.WriteLine("Add one more line ");
sw.WriteLine("Done! ");
}
// Write file contents on console.
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
Read and Write a File
Open a File
A File must be opened using an IO resource before it can be read or write to. A file can be opened to read and/or write purposes. The FileInfo class provides four methods to open a file.
- Open
- OpenRead
- OpenText
- OpenWrite

Chittaranjan SwainPosted Dec 16, 2019, 3:09 AM
Nice article.
Sourav Kumar DasPosted Dec 15, 2019, 11:13 PM
Nice and useful article on File Operation Sir.
Mohamedasiq ShajahanPosted Dec 9, 2019, 7:16 AM
In File.Replace, if we are providing only the file name in the Backupfilename parameter, where will be the backup file stored?
Kim stonegreenPosted Dec 26, 2018, 8:16 AM
Very helpful information. Thanks for sharing.
DeepanPosted Dec 24, 2018, 11:42 PM
All the details explained very well.. thank you sir..
Hamid KhanPosted Dec 22, 2018, 3:43 PM
Good article..................Thanks
Logesh PalaniPosted Dec 22, 2018, 9:47 AM
Superb sir :)
Rehman ShahidPosted Dec 22, 2018, 3:24 AM
Nice Article................
Arjun DhilodPosted Dec 20, 2018, 1:28 AM
Very nice sir thank you for share