Inspect, manage, and navigate the file system in your WinForms apps
When your app needs to inspect files, check sizes, list folders, or handle metadata, System.IO.FileInfo and System.IO.DirectoryInfo give you a powerful, object-oriented way to interact with the filesystem.
Letβs explore how to use them effectively in WinForms.
π§° What Are FileInfo and DirectoryInfo?
They are C# classes that wrap file/folder metadata and actions in easy-to-use objects.
β FileInfo lets you:
- Check size, creation time, last modified time
- Copy, delete, or move files
- Get file extension, path, or name
β DirectoryInfo lets you:
- List all files or folders in a directory
- Create or delete folders
- Get folder details like size, contents, subfolders
π Example: Get Info About a File
private void btnInspectFile_Click(object sender, EventArgs e)
{
string filePath = @"C:\Users\Public\Documents\example.txt";
FileInfo fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
string info = $"Name: {fileInfo.Name}\n" +
$"Size: {fileInfo.Length} bytes\n" +
$"Created: {fileInfo.CreationTime}\n" +
$"Modified: {fileInfo.LastWriteTime}\n" +
$"Extension: {fileInfo.Extension}";
MessageBox.Show(info, "File Info");
}
else
{
MessageBox.Show("File not found.");
}
}
π Example: List All Files in a Folder
private void btnListFiles_Click(object sender, EventArgs e)
{
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DirectoryInfo dir = new DirectoryInfo(folderPath);
FileInfo[] files = dir.GetFiles();
listBox1.Items.Clear();
foreach (FileInfo file in files)
{
listBox1.Items.Add($"{file.Name} - {file.Length / 1024} KB");
}
}
β
You get access to file size, type, and name in one place
β
Also supports filters: dir.GetFiles("*.txt")
π Example: List Subfolders
private void btnListFolders_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Public");
DirectoryInfo[] subDirs = dir.GetDirectories();
listBox1.Items.Clear();
foreach (DirectoryInfo sub in subDirs)
{
listBox1.Items.Add($"{sub.Name} - Created: {sub.CreationTime}");
}
}
β¨ Useful Properties and Methods
πΉ FileInfo
| Property/Method | Description |
|---|---|
Name | File name only |
FullName | Full path |
Length | File size in bytes |
Extension | File type (e.g., .txt) |
CreationTime | When the file was created |
LastWriteTime | When it was last changed |
Delete() | Delete the file |
CopyTo(dest) | Copy the file |
πΉ DirectoryInfo
| Property/Method | Description |
|---|---|
Name | Folder name |
FullName | Full path |
GetFiles() | List of files in the folder |
GetDirectories() | List of subfolders |
Create() | Create the folder |
Delete() | Delete the folder |
π Permissions Note
Attempting to access or delete system-protected files/folders may throw an error. Use try/catch to gracefully handle permission issues.
try
{
fileInfo.Delete();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
π§ͺ Quick Challenge
π§© Build a form with:
- A
TextBoxfor folder path - A
Buttonto list.txtfiles - A
ListBoxto show file names and sizes
Bonus: Add a button to delete selected file using FileInfo.Delete().
π Summary
| Class | Use For |
|---|---|
FileInfo | Get file metadata, delete, copy files |
DirectoryInfo | List files/folders, create directories |
GetFiles() | Returns an array of FileInfo |
GetDirectories() | Returns array of DirectoryInfo |
β Best Practices
- β
Use
Path.Combine()when building paths - β
Always check
Existsbefore working on a file/folder - β Catch IO exceptions (permissions, locks, etc.)
- β Prefer relative or environment-based paths
π Want to Go Further?
- Combine with
OpenFileDialogto inspect any file - Build a mini file explorer
- Sort files by date or size using LINQ
- Add support for renaming or moving files
π¬ Want to build a file manager or a diagnostics tool?
Tell us your idea β Iβll help you wire up FileInfo and DirectoryInfo for a professional experience.