C# File SetCreationTimeUtc() Method

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

Definition

In C#, File.SetCreationTimeUtc() changes the creation time that the file system stores for a file. You pass a DateTime that is read as UTC, so give it DateTimeKind.Utc.

Only the timestamp changes; the content of the file is not touched. The file must already exist. Setting a fixed time is the reliable way to test code that depends on file age, because the real time keeps moving. The local form is SetCreationTime.

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

Syntax

Pass the path of an existing file and the new DateTime.

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

File.SetCreationTimeUtc(path, new DateTime(2026, 1, 15, 9, 30, 0, DateTimeKind.Utc));

Console.WriteLine(File.GetCreationTimeUtc(path).Year);   // 2026
CallWhat it does
File.SetCreationTimeUtc(path, time)Sets the creation time to time.
File.SetCreationTimeUtc(path, DateTime.UtcNow)Sets it to the current moment, a "touch".
File.SetCreationTimeUtc(path, new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc))Gives the file a fixed old date, handy for testing.

FileInfo.CreationTimeUtc is a property you can also assign. The static call needs only the path, which is handy for one quick change.

Library declarations
C#
public static void SetCreationTimeUtc(string path, DateTime creationTimeUtc);

Parameters

ParameterDescription
pathThe file to change. It must exist.
creationTimeUtcThe new creation time. Treated as UTC; use DateTimeKind.Utc.

Return Value

Return ValueDescription
voidNothing is returned. Read the result back with GetCreationTimeUtc.

C# Examples

Example 1 — Set a UTC Creation Time and Read the Exact Text

Use the round-trip format "o" to see the full UTC value.

C#
using System;
using System.IO;

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

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

        DateTime stamp = new DateTime(2026, 1, 15, 9, 30, 0, DateTimeKind.Utc);
        File.SetCreationTimeUtc(path, stamp);

        Console.WriteLine(File.GetCreationTimeUtc(path).ToString("o"));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A UTC value on purpose. The fifth argument DateTimeKind.Utc says that 09:30 is UTC, so no time zone changes it.
  2. Set it. SetCreationTimeUtc stores the moment as it is. The file content is not touched.
  3. The round-trip format. "o" prints year to fractions of a second, and the final Z marks UTC. The output is identical on every computer.

Example 2 — The Same Moment as Local Time

Read a UTC value through the local method and compare.

C#
using System;
using System.IO;

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

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

        DateTime stamp = new DateTime(2026, 1, 15, 9, 30, 0, DateTimeKind.Utc);
        File.SetCreationTimeUtc(path, stamp);

        DateTime local = File.GetCreationTime(path);
        Console.WriteLine(local == stamp.ToLocalTime());
        Console.WriteLine(local.Kind);

        Directory.Delete(root, true);
    }
}

How It Works

  1. One stored moment. Whether you read it with GetCreationTime or GetCreationTimeUtc, the file system keeps one value.
  2. Convert to compare. stamp.ToLocalTime() moves the UTC stamp into your time zone, and the result equals what the local getter returns.
  3. The kind changes with the method. The local getter marks its result as Local, even though the value was set in UTC.

Example 3 — Setting the Time of a Missing File

See that the file must exist before its creation time can be set.

C#
using System;
using System.IO;

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

        string missing = Path.Combine(root, "ghost.txt");
        DateTime stamp = new DateTime(2026, 1, 15, 9, 30, 0, DateTimeKind.Utc);

        try {
            File.SetCreationTimeUtc(missing, stamp);
        } catch (IOException) {
            Console.WriteLine("Cannot set the time of a missing file");
        }

        File.WriteAllText(missing, "now it exists");
        File.SetCreationTimeUtc(missing, stamp);
        Console.WriteLine("Set after creating the file");

        Directory.Delete(root, true);
    }
}

How It Works

  1. Nothing to change. ghost.txt does not exist, so there is no creation time to replace.
  2. An IOException. The call throws FileNotFoundException, which is a kind of IOException, and the catch block prints the message. This is different from the getter, which stays quiet.
  3. Create first. After WriteAllText makes the file, the same call succeeds, and the last line is printed.

Frequently Asked Questions

It changes the creation time of a file to the DateTime you pass, without changing the content.
Use this Utc form when your value is already UTC or must be exact on every machine. The local form SetCreationTime is for values that come from the user's own clock.
The call throws an IOException (FileNotFoundException). Create the file first, or check File.Exists.
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.GetCreationTimeUtc(path). Pass a fixed DateTime when you set it, so the value you read back is predictable.

Next: SetLastAccessTime()

Change the last access time of a file using a local DateTime.

SetLastAccessTime() 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