How to Get File Info in C#: Simple Guide
In C#, you can get file information using the
FileInfo class from System.IO. Create a FileInfo object with the file path, then access properties like Length, CreationTime, and Attributes to get details about the file.Syntax
The FileInfo class provides properties and methods to get details about a file. You create an instance by passing the file path as a string to its constructor.
FileInfo file = new FileInfo(string filePath);- creates a new FileInfo object for the file.- Use properties like
file.Lengthfor size,file.CreationTimefor creation date, andfile.Attributesfor file attributes.
csharp
using System.IO; FileInfo file = new FileInfo("path/to/your/file.txt"); long size = file.Length; DateTime created = file.CreationTime; FileAttributes attrs = file.Attributes;
Example
This example shows how to get and print the size, creation time, and attributes of a file using FileInfo.
csharp
using System; using System.IO; class Program { static void Main() { string filePath = "example.txt"; // Create a sample file to ensure it exists if (!File.Exists(filePath)) { File.WriteAllText(filePath, "Hello, world!"); } FileInfo file = new FileInfo(filePath); Console.WriteLine($"File: {file.Name}"); Console.WriteLine($"Size (bytes): {file.Length}"); Console.WriteLine($"Created: {file.CreationTime}"); Console.WriteLine($"Attributes: {file.Attributes}"); } }
Output
File: example.txt
Size (bytes): 13
Created: 6/1/2024 10:00:00 AM
Attributes: Archive
Common Pitfalls
Common mistakes when getting file info include:
- Using a file path that does not exist, which causes exceptions.
- Not handling exceptions like
FileNotFoundExceptionorUnauthorizedAccessException. - Confusing
FileInfoproperties with staticFileclass methods.
Always check if the file exists before accessing its info and use try-catch blocks to handle errors gracefully.
csharp
using System; using System.IO; class Program { static void Main() { string filePath = "missing.txt"; // Wrong way: no check, may throw exception // FileInfo file = new FileInfo(filePath); // Console.WriteLine(file.Length); // Right way: if (File.Exists(filePath)) { FileInfo file = new FileInfo(filePath); Console.WriteLine(file.Length); } else { Console.WriteLine("File does not exist."); } } }
Output
File does not exist.
Quick Reference
| Property/Method | Description |
|---|---|
| Length | Gets the size of the file in bytes. |
| CreationTime | Gets or sets the creation date and time of the file. |
| LastAccessTime | Gets or sets the last access date and time. |
| LastWriteTime | Gets or sets the last write date and time. |
| Attributes | Gets or sets the file attributes (e.g., ReadOnly, Hidden). |
| Exists | Checks if the file exists (bool). |
| Delete() | Deletes the file. |
Key Takeaways
Use the FileInfo class to get detailed information about a file in C#.
Always check if the file exists before accessing its properties to avoid exceptions.
FileInfo provides properties like Length, CreationTime, and Attributes for common file info.
Handle exceptions to manage cases like missing files or permission issues.
FileInfo objects are created by passing the file path to the constructor.