C# File GetCreationTime() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, File.GetCreationTime() returns the creation time of a file: the moment it was created. The answer is a DateTime in the computer's local time zone (Kind is Local).

The method does not throw for a missing file. It returns 1601-01-01 00:00 UTC converted to local time, so check File.Exists first. For comparisons between computers, prefer GetCreationTimeUtc, which is not affected by time zones.

This is a static method of System.IO.File. Call it as File.GetCreationTime(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Pass the path of an existing file and store the DateTime that comes back.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-getcreationtime.txt");
File.WriteAllText(path, "demo");

DateTime when = File.GetCreationTime(path);

Console.WriteLine(when.Kind);   // Local
CallWhat it does
File.GetCreationTime(path)Returns the creation time as local time.
File.GetCreationTime(path).ToString("yyyy-MM-dd HH:mm")Formats the value as text with a fixed pattern.
File.GetCreationTime(missingPath)No exception. Returns 1601-01-01 UTC in local time.

FileInfo.CreationTime gives the same value as a property. A FileInfo may keep an older value until you call Refresh(), while File.GetCreationTime asks the file system every time.

Library declarations
C#
public static DateTime GetCreationTime(string path);

Parameters

ParameterDescription
pathThe file to read the time from. It may be absolute or relative.

Return Value

Return ValueDescription
DateTimeThe creation time, with Kind set to Local. For a missing file the value is January 1, 1601 UTC, converted to local time.

C# Examples

Example 1 — Read a Creation Time You Set Yourself

The real creation time keeps changing, so set a fixed time first and then read it with GetCreationTime.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getcreationtime");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "stamp.txt");
        File.WriteAllText(path, "demo");

        File.SetCreationTime(path, new DateTime(2026, 1, 15, 9, 30, 0));

        DateTime created = File.GetCreationTime(path);
        Console.WriteLine(created.ToString("yyyy-MM-dd HH:mm"));
        Console.WriteLine(created.Kind);

        Directory.Delete(root, true);
    }
}

How It Works

  1. A fixed time for a fixed output. SetCreationTime puts a known value on the file, so the program prints the same lines on every run.
  2. Read it back. GetCreationTime returns the value as local time, which is the same wall clock time that was set.
  3. Format and kind. ToString("yyyy-MM-dd HH:mm") prints a fixed layout, and Kind shows Local, which is what this method always returns.

Example 2 — What a Missing File Returns

Ask for the creation time of a file that is not there and look at the year.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getcreationtime-missing");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string missing = Path.Combine(root, "missing.txt");

        Console.WriteLine(File.Exists(missing));

        DateTime created = File.GetCreationTime(missing);
        Console.WriteLine(created.ToUniversalTime().Year);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Nothing to read. missing.txt was never created, so Exists prints False.
  2. No exception. GetCreationTime does not complain. It returns a special default value instead.
  3. The default is the year 1601. The value is 1 January 1601 UTC shown in local time. Converting back with ToUniversalTime() before reading Year prints 1601 in every time zone.

Example 3 — The Local Value Is the Utc Value Converted

Compare GetCreationTime with GetCreationTimeUtc on the same file.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getcreationtime-utc");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "pair.txt");
        File.WriteAllText(path, "demo");
        File.SetCreationTime(path, new DateTime(2026, 1, 15, 9, 30, 0));

        DateTime local = File.GetCreationTime(path);
        DateTime utc = File.GetCreationTimeUtc(path);

        Console.WriteLine(utc.ToLocalTime() == local);
        Console.WriteLine(local.Date == new DateTime(2026, 1, 15));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two views of one value. The file stores one moment in time. The local method and the Utc method only show it in two time zones.
  2. Convert and compare. utc.ToLocalTime() moves the UTC value into local time, so it equals the value from GetCreationTime.
  3. The local date matches what you set. local.Date drops the hour and minute, so it equals 15 January 2026 in the computer's own time zone.

Frequently Asked Questions

A DateTime with the creation time of the file, as local time (Kind is Local).
Use the Utc form for comparisons, storage, and anything shared between computers. Use GetCreationTime when you only show the time to the user in their own time zone.
It does not throw. It returns January 1, 1601 UTC, converted to local time. Check File.Exists first when a missing file is possible.
Many Linux file systems do not store a creation time, or .NET cannot read or change it there. The value may be approximated from other times. For portable code use the last write time, and expect creation-time code to work best on Windows and macOS.
Call File.SetCreationTime(path, dateTime). Pass a fixed DateTime when you want a predictable value for tests.

Next: GetCreationTimeUtc()

Read when a file was created, as UTC time that does not depend on the time zone.

GetCreationTimeUtc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful