Search code examples
c#.netfile-iotimerobocopy

.Net library to move / copy a file while preserving timestamps


Does anyone know of a .Net library where a file can be copied / pasted or moved without changing any of the timestamps. The functionality I am looking for is contained in a program called robocopy.exe, but I would like this functionality without having to share that binary.

Thoughts?


Solution

  • public static void CopyFileExactly(string copyFromPath, string copyToPath)
    {
        var origin = new FileInfo(copyFromPath);
    
        origin.CopyTo(copyToPath, true);
    
        var destination = new FileInfo(copyToPath);
        destination.CreationTime = origin.CreationTime;
        destination.LastWriteTime = origin.LastWriteTime;
        destination.LastAccessTime = origin.LastAccessTime;
    }