In this guide, we will discuss the FileInfo class in C# and demonstrate its various uses.
FileInfo Class in C#
Fileinfo is a class in the System.IO namespace through which you can access file attributes and operations in C#. The Fileinfo class represents a file on disk and provides methods, such as reading, writing, and manipulating files. In addition, It also enables you to retrieve information about a file, such as its size, creation time, and last modified time.
To utilize the FileInfo class, first, create an instance of it and pass in the path to the file to work with. As an example:
Properties of Fileinfo in C#
To learn about a file in the file system in C#, we use FileInfo. The FileInfo class contains properties such as Name, Length, CreationTime, and LastWriteTime. These properties provide information about the file name, size, and modification times, among other things. We can create an instance of the FileInfo class using the file path and name, and then use the various properties to retrieve information about the file.
Property | Description |
Attributes | It is used to retrieve or set the attributes of the currently selected file or directory. |
CreationTime | It is used to get or set the time when the current file or directory was created. |
Directory | It is used to retrieve a parent directory instance. |
DirectoryName | It is used to obtain a string representing the complete path of the directory. |
Exists | It returns a value indicating whether or not a file exists. |
FullName | It is used to obtain the whole path of a directory or file. |
IsReadOnly | It is used to get or set a value indicating whether the current file is read-only. |
LastAccessTime | It is used to get or set the last time a file or directory in the current directory was accessed. |
Length | It is used to determine the current file’s size in bytes. |
Name | It is used to obtain the file’s name. |
Here is the code example of properties of FileInfo in C#.
using System.IO;
class Program
{
static void Main(string[] args)
{
FileInfo file = new FileInfo("LinuxHint.txt");
if (file.Exists)
{
Console.WriteLine("File name: " + file.Name);
Console.WriteLine("Full path: " + file.FullName);
Console.WriteLine("Size (bytes): " + file.Length);
Console.WriteLine("Last write time: " + file.LastWriteTime);
file.Delete();
Console.WriteLine("File deleted.");
}
else
{
Console.WriteLine("File not found.");
}
Console.ReadLine();
}
}
This code generates a new FileInfo object for the file LinuxHint.txt. It then determines whether the file exists and, if so, displays some of its information, including the name, full path, size in bytes, and last write time. Finally, it uses the delete function to delete the original file.
Output
Methods of FileInfo in C#
The FileInfo class also provides various methods to interact with the file system. These methods include:
Methods | Description |
AppendText() | Appends text to the end of the file. |
CopyTo() | Copies the file to a new location. |
Create() | Creates a new file. |
Delete() | Deletes the file. |
Exists() | Returns a Boolean value indicating whether the file exists. |
MoveTo() | Moves the file to a new location. |
Open() | Opens the file in the specified mode. |
OpenRead() | Opens the file in read-only mode. |
OpenText() | Opens the file for reading as text |
OpenWrite() | Opens the file for writing. |
Refresh() | Refreshes the state of the file. |
Replace() | Replaces the file with a new file. |
ToString() | Returns the path of the file as a string |
Here is the code example of methods of FileInfo in C#:
static void Main(string[] args)
{
FileInfo fileInfo = new FileInfo("LinuxHint.txt");
Console.WriteLine("File created.");
StreamWriter streamWriter = fileInfo.AppendText();
streamWriter.Write("Linux Hint!");
streamWriter.Close();
Console.WriteLine("Contents Written in File.");
if (fileInfo.Exists)
{
Console.WriteLine("File exists.");
}
FileStream fileStream2 = fileInfo.OpenRead();
fileStream2.Close();
Console.WriteLine("File opened in Read Only Mode.");
StreamReader streamReader = fileInfo.OpenText();
streamReader.Close();
Console.WriteLine("File read as text.");
FileStream fileStream3 = fileInfo.OpenWrite();
fileStream3.Close();
fileInfo.Refresh();
Console.WriteLine("File refreshed.");
string path = fileInfo.ToString();
Console.WriteLine("The path of the file is: "+ path);
Console.ReadLine();
}
}
The above code creates a new file called LinuxHint.txt and appends the text Linux Hint! to it with the AppendText() function. It then uses the Exists() function to see if the file exists before opening it in read-only mode with the OpenRead() method. The OpenText() method is also used to open the file as text. The code then uses the Refresh() method to refresh the file’s status and the ToString() method to obtain the file’s path. Finally, before quitting, it prints the path.
Output
Conclusion
The FileInfo class in C# provides a powerful tool for interacting with files in the file system. The properties and methods of the FileInfo class allow us to retrieve information about files and manipulate them efficiently. The ability to interact with the file system is an essential part of most applications, and the FileInfo class simplifies this task. With the FileInfo class, we can easily create file management applications and add file management functionality to our existing applications.