FileInfo in C# is a class in the System.IO namespace that provides properties and methods for working with files.
It allows you to get information about files (such as size, creation date, etc.) and to perform file operations like copy, delete, and move.
Unlike File (which is static), FileInfo requires an instance of the file, allowing you to manipulate a specific file.
In this tutorial, we’ll cover:
1. Basics of FileInfo
FileInfo is an instance-based class, which means you need to create an object of FileInfo for each file you want to work with. The FileInfo constructor accepts a file path, and it provides properties and methods for interacting with that specific file.
Example: Basic FileInfo Setup
using System; using System.IO; public class FileInfoExample { public static void Main() { FileInfo fileInfo = new FileInfo("sample.txt"); // Perform operations on the file using fileInfo } }
In this example, fileInfo represents the file sample.txt, and you can use it to get information about or perform operations on that file.
2. Retrieving File Information
FileInfo provides several properties to retrieve information about a file, such as its size, creation date, last access date, and more.
Example: Getting File Information
using System; using System.IO; public class FileInfoGetExample { public static void Main() { FileInfo fileInfo = new FileInfo("sample.txt"); if (fileInfo.Exists) { Console.WriteLine("File Name: " + fileInfo.Name); Console.WriteLine("Full Path: " + fileInfo.FullName); Console.WriteLine("Directory: " + fileInfo.DirectoryName); Console.WriteLine("Size: " + fileInfo.Length + " bytes"); Console.WriteLine("Created On: " + fileInfo.CreationTime); Console.WriteLine("Last Accessed: " + fileInfo.LastAccessTime); Console.WriteLine("Last Modified: " + fileInfo.LastWriteTime); } else { Console.WriteLine("File does not exist."); } } }
Explanation:
- fileInfo.Exists checks if the file exists.
- Name, FullName, DirectoryName, Length, CreationTime, LastAccessTime, and LastWriteTime are properties that retrieve information about the file.
Output (example):
File Name: sample.txt Full Path: C:\path\to\sample.txt Directory: C:\path\to Size: 1024 bytes Created On: 10/30/2024 10:00:00 AM Last Accessed: 10/30/2024 11:00:00 AM Last Modified: 10/30/2024 10:15:00 AM
3. Creating, Copying, and Deleting Files with FileInfo
FileInfo provides methods to create, copy, and delete files.
Example: Creating a File
using System; using System.IO; public class FileInfoCreateExample { public static void Main() { FileInfo fileInfo = new FileInfo("newfile.txt"); using (FileStream fs = fileInfo.Create()) { Console.WriteLine("File created: " + fileInfo.FullName); } } }
Explanation:
- fileInfo.Create() creates a new file and returns a FileStream.
- The file is created at the specified path (newfile.txt).
Example: Copying a File
using System; using System.IO; public class FileInfoCopyExample { public static void Main() { FileInfo fileInfo = new FileInfo("newfile.txt"); if (fileInfo.Exists) { string destinationPath = "copy_of_newfile.txt"; fileInfo.CopyTo(destinationPath, overwrite: true); Console.WriteLine("File copied to: " + destinationPath); } else { Console.WriteLine("Source file does not exist."); } } }
Explanation:
- CopyTo(destinationPath, overwrite: true) copies the file to copy_of_newfile.txt, overwriting it if it exists.
Example: Deleting a File
using System; using System.IO; public class FileInfoDeleteExample { public static void Main() { FileInfo fileInfo = new FileInfo("copy_of_newfile.txt"); if (fileInfo.Exists) { fileInfo.Delete(); Console.WriteLine("File deleted: " + fileInfo.FullName); } else { Console.WriteLine("File does not exist."); } } }
Explanation:
- Delete() deletes the specified file if it exists.
4. Moving and Renaming Files
You can use FileInfo.MoveTo() to move a file to a different directory or rename it by specifying a new path.
Example: Moving and Renaming a File
using System; using System.IO; public class FileInfoMoveExample { public static void Main() { FileInfo fileInfo = new FileInfo("newfile.txt"); if (fileInfo.Exists) { string newFilePath = "moved_and_renamed_file.txt"; fileInfo.MoveTo(newFilePath); Console.WriteLine("File moved and renamed to: " + newFilePath); } else { Console.WriteLine("File does not exist."); } } }
Explanation:
- MoveTo(newFilePath) moves the file to the specified path. This can also be used to rename the file.
5. Practical Example: Checking File Properties
In this example, we’ll check several properties of a file to confirm if it’s read-only or hidden.
Example: Checking File Attributes
using System; using System.IO; public class FileInfoAttributesExample { public static void Main() { FileInfo fileInfo = new FileInfo("sample.txt"); if (fileInfo.Exists) { Console.WriteLine("File Attributes: " + fileInfo.Attributes); if ((fileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { Console.WriteLine("The file is read-only."); } else { Console.WriteLine("The file is not read-only."); } if ((fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { Console.WriteLine("The file is hidden."); } else { Console.WriteLine("The file is not hidden."); } } else { Console.WriteLine("File does not exist."); } } }
Explanation:
- fileInfo.Attributes gives the file attributes (e.g., ReadOnly, Hidden).
- FileAttributes.ReadOnly and FileAttributes.Hidden check if the file is read-only or hidden.
Output (example):
File Attributes: Archive, ReadOnly The file is read-only. The file is not hidden.
Advanced Example: Changing File Attributes
You can modify the file attributes using FileInfo.Attributes. For example, you can make a file read-only or hidden.
Example: Modifying File Attributes
using System; using System.IO; public class FileInfoChangeAttributesExample { public static void Main() { FileInfo fileInfo = new FileInfo("sample.txt"); if (fileInfo.Exists) { // Make the file read-only fileInfo.Attributes |= FileAttributes.ReadOnly; Console.WriteLine("File is now read-only."); // Remove the read-only attribute fileInfo.Attributes &= ~FileAttributes.ReadOnly; Console.WriteLine("File is no longer read-only."); // Make the file hidden fileInfo.Attributes |= FileAttributes.Hidden; Console.WriteLine("File is now hidden."); } else { Console.WriteLine("File does not exist."); } } }
Explanation:
- fileInfo.Attributes |= FileAttributes.ReadOnly adds the read-only attribute.
- fileInfo.Attributes &= ~FileAttributes.ReadOnly removes the read-only attribute.
- fileInfo.Attributes |= FileAttributes.Hidden adds the hidden attribute.
Summary
FileInfo in C# provides a powerful, object-oriented way to work with files and retrieve detailed information. Here’s a summary of the key points:
- Basic Setup: Create a FileInfo object for a specific file by providing its path.
- Retrieving Information: Properties like Name, FullName, Length, CreationTime, LastAccessTime, and Attributes provide various details about the file.
- File Operations:
- Create(): Creates a new file.
- CopyTo(): Copies the file to a new location.
- Delete(): Deletes the file.
- MoveTo(): Moves or renames the file.
- Checking and Modifying Attributes: Attributes can be used to check if a file is read-only, hidden, etc., and modify these properties.
FileInfo is a useful class for managing files within applications, providing control over file attributes and supporting a range of operations.