In this article, we’ll walk through the creation of a simple yet practical .NET console application that lists all files modified in the last hour within a specified directory. This is a great starting point for developers who want to automate file monitoring tasks or integrate such logic into larger systems.
🔍 Building a Simple .NET Console App to List Recently Modified Files
Author: Edvaldo Guimarães\ Category: .NET / C# / Productivity Tools\ Tags: .NET 6, Console App, File System, C#, Visual Studio
🧠 Introduction
In this article, we’ll walk through the creation of a simple yet practical .NET console application that lists all files modified in the last hour within a specified directory. This is a great starting point for developers who want to automate file monitoring tasks or integrate such logic into larger systems.
We’ll use .NET 6 and Visual Studio Community 2022, following a clean and beginner-friendly approach.
🛠️ Tools & Requirements
- Visual Studio Community 2022
- .NET 6 SDK or later
- Basic knowledge of C# and file system operations
🚀 Step-by-Step Guide
1. Create the Project
Open Visual Studio and follow these steps:
- Click “Create a new project”
- Search for “Console App”
- Select “Console App” (.NET 6 or later)
- Name the project:
FileLister - Choose a location and click Create
2. Write the Code
Replace the contents of Program.cs with the following:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
// Use argument or default path
string directoryPath = args.Length > 0 ? args[0] : @"C:\Your\Directory";
if (!Directory.Exists(directoryPath))
{
Console.WriteLine($"Directory not found: {directoryPath}");
return;
}
Console.WriteLine($"Files modified in the last hour in: {directoryPath}\n");
DateTime oneHourAgo = DateTime.Now.AddHours(-1);
var files = Directory.GetFiles(directoryPath);
foreach (var file in files)
{
DateTime lastWriteTime = File.GetLastWriteTime(file);
if (lastWriteTime >= oneHourAgo)
{
Console.WriteLine($"{Path.GetFileName(file)} - Modified: {lastWriteTime}");
}
}
}
}
3. Run the App
Click the Start button or press F5. The console will display files modified in the last hour from the default or specified directory.
4. Pass a Directory as Argument
To test with a custom directory:
- Right-click the project → Properties
- Go to the Debug tab
- Add your directory path in Command line arguments, e.g.:
C:\Users\Edvaldo\Documents
Run the app again to see results from the new path.
✅ Why Use .NET 6 for Console Apps?
.NET 6 (and newer versions) offer:
- Cross-platform support
- Better performance
- Modern language features
- Long-term support (LTS)
For new projects, especially CLI tools, .NET Core/.NET 6+ is the recommended choice over the legacy .NET Framework.
🧩 What’s Next?
You can extend this app by:
- Exporting results to
.csvor.txt - Adding filters by file extension
- Recursively scanning subdirectories
- Creating a GUI version with WinForms or WPF
📌 Conclusion
This simple console app is a great example of how .NET can be used to automate file system tasks. Whether you’re building tools for personal use or integrating with enterprise systems, starting with a clean and modular CLI app is a smart move.
