C# FileInfo

By FoxLearn 12/12/2024 3:47:58 AM   190
FileInfo is a class in .NET that provides properties and methods for managing files.

It allows you to create, copy, delete, move, and open files. Additionally, it helps in creating FileStream objects for working with file data.

C# FileInfo Example

We create an instance of the FileInfo class for the file 'sample.txt'. This object will allow you to retrieve details about the file.

var fi = new FileInfo("c:/dell/sample.txt"); // Create a FileInfo object for the specified file
Console.WriteLine(fi.Name); // Print the file name (without the full path)
Console.WriteLine(fi.FullName); // Print the full path of the file
bool readOnly = fi.IsReadOnly; // Check if the file is read-only

Output:

sample.txt
c:\dell\sample.txt
false  // If the file is not read-only

The IsReadOnly property returns a boolean value (true or false) depending on whether the file is marked as read-only in the file system.

C# FileInfo create text file

The CreateText method creates a StreamWriter that is used to write to a new text file. If the file already exists, it will be overwritten.

var fi = new FileInfo("c:/dell/sample.txt"); // Create a FileInfo object for the file "sample.txt"
using StreamWriter sw = fi.CreateText(); // Create a StreamWriter to write to the file (creates the file if it doesn't exist)
sw.WriteLine("c# programming"); // Write the first line to the file
sw.WriteLine("c# fileinfo"); // Write the second line to the file

We create a new text file called sample.txt and write two lines of text into it. The StreamWriter class implements the IDisposable interface, so we use the using statement to automatically close and release resources when done.

C# FileInfo read file

The OpenText method creates a StreamReader with UTF-8 encoding to read from an existing text file.

var fi = new FileInfo("c:/dell/sample.txt"); // Create a FileInfo object for the specified file
using StreamReader streamReader = fi.OpenText(); // Open the file for reading text
string? str = string.Empty; // Initialize a variable to store each line of text from the file
while ((str = streamReader.ReadLine()) != null) // Loop through the file line by line
{
    Console.WriteLine(str); // Print each line to the console
}

In the example, we read a file line by line.

C# FileInfo delete file

You can use the Delete method to delete a file.

var fi = new FileInfo("c:/dell/app.config"); // Create a FileInfo object for the file "app.config"
try
{
    fi.Delete(); // Attempt to delete the file
}
catch (UnauthorizedAccessException ex)
{
    Console.WriteLine(ex.Message);
}

In the example, we attempt to delete a system file, but an exception is thrown due to insufficient permissions.

C# FileInfo copy file

The CopyTo method copies an existing file to a new location, preventing the overwriting of an existing file.

var fi = new FileInfo("C:\\Dell\\web.config"); // Create a FileInfo object for the source file
fi.CopyTo("C:\\Dell\\app.config"); // Copy the file to the destination path

The example creates a copy of the web.config file.

C# FileInfo Directory

The Directory gets an instance of the parent directory.

var fi = new FileInfo("C:\\Dell\\Web.config");
var dir = fi.Directory; // This property returns a DirectoryInfo object for the directory that contains the file.
Console.WriteLine(dir?.FullName); // This will print the full path of the directory that contains the file.
Console.WriteLine(dir?.LastAccessTime); // This will print the last access time of the directory.
Console.WriteLine(dir?.Root); // This will print the root directory of the drive where the file is located.

Output:

C:\Dell
12/12/2024 10:30:52 AM
C:\

The example prints the file's parent directory's full name, last access time, and root directory.