Share via

How to verify same creation time in C# after copy file from source to target

Pip 265 Reputation points
2026-02-19T20:30:00.9066667+00:00

Hi,

I'm using C# in .NET 10.

1.How to write a function VerifySameCreationTime that verify verify same creation time in C#, as results of input file that pass operation of copy to output path.

2.In this case the creation time is change but modified time not change , why ?

3.This what Chat GPT suggest, is it ok? I need tolerance? if yes how i know how much?

Thanks,

  var inputCreation = File.GetCreationTimeUtc(inputPath);
  var outputCreation = File.GetCreationTimeUtc(outputPath);

  logger?.Debug($"VerifySameCreationTime: InputCreationUtc={inputCreation:o}, OutputCreationUtc={outputCreation:o}");

  // Allow a small tolerance to account for filesystem timestamp resolution and clock skew
  var tolerance = TimeSpan.FromSeconds(2);
  var outputIsNewer = outputCreation - inputCreation > tolerance;

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

{count} votes

Answer accepted by question author
  1. Marcin Policht 81,705 Reputation points MVP Volunteer Moderator
    2026-02-19T20:53:27.3533333+00:00

    When you copy a file, the behavior of creation and modified timestamps depends on the API used and the underlying file system. On Windows NTFS, when you copy a file using typical APIs like File.Copy, the new file gets a new creation time (the moment the copy is created), but the last write time (modified time) is usually preserved from the source file. That is why you see the creation time change but the modified time remain the same. Creation time represents when that specific file entry was created in that filesystem location, not when the content was originally created.

    If your goal is to verify that the copy operation preserved the original creation time, you need to be aware that File.Copy does not guarantee preservation of creation time. If you require it to match, you must explicitly set it after copying:

    File.Copy(inputPath, outputPath, overwrite: true);
    
    var inputCreation = File.GetCreationTimeUtc(inputPath);
    File.SetCreationTimeUtc(outputPath, inputCreation);
    

    To implement a function VerifySameCreationTime, you should compare the UTC timestamps and optionally allow a tolerance to account for filesystem timestamp resolution differences. Different filesystems have different timestamp precision. NTFS supports 100-nanosecond resolution, but other filesystems (for example FAT) have 2-second resolution for certain timestamps. Network shares and cross-platform copies can also introduce truncation.

    One potential implementation would be:

    public static bool VerifySameCreationTime(string inputPath, string outputPath, TimeSpan? tolerance = null)
    {
        var inputCreation = File.GetCreationTimeUtc(inputPath);
        var outputCreation = File.GetCreationTimeUtc(outputPath);
    
        var allowedDrift = tolerance ?? TimeSpan.Zero;
    
        return Math.Abs((outputCreation - inputCreation).Ticks) <= allowedDrift.Ticks;
    }
    

    Whether you need tolerance depends on your environment. If both files are on NTFS on the same machine and you explicitly set the creation time, you usually do not need tolerance. If files may reside on FAT, SMB shares, Linux filesystems, or cloud-mounted storage, you should allow tolerance.

    How much tolerance to use depends on the lowest precision filesystem you expect. If FAT is involved, up to 2 seconds is reasonable. If only NTFS is involved, you can often use zero or a very small tolerance like a few milliseconds. If you do not control the storage backend, I'd consider 1–2 seconds to be a pragmatic safe value.

    The snippet ChatGPT suggested:

    var tolerance = TimeSpan.FromSeconds(2);
    var outputIsNewer = outputCreation - inputCreation > tolerance;
    

    is not sufficient to verify “same creation time” because it only checks whether the output is significantly newer. It does not check the absolute difference in both directions.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.