.NET
file management
creation date
programming
software development

Getting files by creation date in .NET

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In .NET, you can filter or sort files by creation time through FileInfo or the static File APIs. The important caveat is that “creation date” is not equally meaningful on every filesystem and operating system. So the code is easy, but you still need to know whether the metadata you are relying on is actually trustworthy in your environment.

Use DirectoryInfo and FileInfo for Rich Metadata

A common pattern is to enumerate files through DirectoryInfo and then filter by CreationTime or CreationTimeUtc.

csharp
1using System;
2using System.IO;
3using System.Linq;
4
5class Program
6{
7    static void Main()
8    {
9        var directory = new DirectoryInfo(@"C:\Temp");
10        var cutoff = new DateTime(2024, 1, 1);
11
12        var files = directory.GetFiles()
13            .Where(f => f.CreationTime >= cutoff)
14            .OrderBy(f => f.CreationTime);
15
16        foreach (var file in files)
17        {
18            Console.WriteLine($"{file.Name} - {file.CreationTime}");
19        }
20    }
21}

This is often the clearest API because FileInfo gives you both metadata and file identity in one object.

Prefer UTC When Time Zones Matter

If your application runs across time zones, servers, or containers, CreationTimeUtc is usually safer than local time.

csharp
var files = directory.GetFiles()
    .Where(f => f.CreationTimeUtc >= DateTime.UtcNow.AddDays(-7));

Using UTC avoids bugs where local machine settings or daylight-saving transitions make time-based comparisons harder to reason about.

Sorting by Creation Date Is Often as Important as Filtering

Sometimes you do not want only files newer than a threshold. You want the newest or oldest files first.

csharp
var newestFirst = directory.GetFiles()
    .OrderByDescending(f => f.CreationTimeUtc)
    .ToList();

This is useful for cleanup tasks, archival jobs, or picking the latest generated artifact from a directory.

Be Careful: Creation Time Is Not Portable Truth

The major operational caveat is that creation time metadata is not equally stable everywhere. On some systems:

  • creation time may not exist as a true filesystem concept
  • file copies may reset or alter creation time
  • extracted archives may get fresh timestamps
  • network-mounted filesystems may behave differently from local disks

So if you need a trustworthy chronological business timestamp, filesystem metadata may be the wrong source. A database field or application-managed timestamp may be more reliable.

Enumerating Large Directories Efficiently

GetFiles() returns all file entries immediately, which can be expensive for very large directories. If you need more scalable enumeration, prefer EnumerateFiles().

csharp
var recentPaths = Directory.EnumerateFiles(@"C:\Temp")
    .Select(path => new FileInfo(path))
    .Where(f => f.CreationTimeUtc >= DateTime.UtcNow.AddDays(-1));

This lets you process files lazily instead of materializing the whole directory listing at once.

Search Recursively When Needed

If the task includes subdirectories, add recursive search.

csharp
1var files = Directory.EnumerateFiles(
2    @"C:\Temp",
3    "*.*",
4    SearchOption.AllDirectories)
5    .Select(path => new FileInfo(path))
6    .OrderBy(f => f.CreationTimeUtc);

For big trees, pair this with error handling because permission issues and inaccessible folders are common.

Handle Exceptions in Real Systems

File enumeration can fail for reasons unrelated to the date filter itself:

  • missing directory
  • permission denied
  • path too long in some environments
  • files deleted between enumeration and inspection

So production code usually wraps filesystem access in targeted exception handling rather than assuming metadata calls always succeed.

Common Pitfalls

The most common mistake is assuming creation time has the same meaning on every platform or filesystem. It often does not.

Another mistake is using local CreationTime when UTC comparisons would be safer and easier to reason about.

Developers also use GetFiles() on huge directories when lazy enumeration with EnumerateFiles() would be more memory-friendly.

Summary

  • In .NET, you can filter or sort files by creation time with FileInfo and DirectoryInfo.
  • 'CreationTimeUtc is usually better than local time for stable comparisons.'
  • Use EnumerateFiles() when scanning large directories.
  • Treat filesystem creation time as metadata with platform caveats, not as universally authoritative business data.
  • Add error handling if the code runs outside simple local development scenarios.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.