[C#] How to Get and Change File Creation and Update Times (FileInfo / File)

The “Last Write Time” (timestamp) of a file is essential for tasks like backup processing, automatically deleting old logs, or managing cache freshness. By using the System.IO.FileInfo class or the static methods of the File class, you can both retrieve and modify these date and time values.

目次

Implementation Sample: Simulating Old Files

The following code demonstrates how to change a test file’s update time to “one year ago.” This is useful for testing batch processes designed to delete old files.

Sample Code

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        // Target file path
        string filePath = "test_cleanup_target.dat";

        // 1. Preparation: Create a new file (the timestamp will be "Now")
        if (!File.Exists(filePath))
        {
            File.WriteAllText(filePath, "Old data content...");
        }

        // Create a FileInfo instance
        var fileInfo = new FileInfo(filePath);

        // Display timestamps before changes
        Console.WriteLine($"[Before] Last Write Time: {fileInfo.LastWriteTime}");
        Console.WriteLine($"[Before] Creation Time: {fileInfo.CreationTime}");
        Console.WriteLine("--- Modifying timestamps ---");

        // 2. Change update and creation times to "one year ago"
        // This is effective when you need a file to be treated as "old" for testing
        DateTime oldDate = DateTime.Now.AddYears(-1);

        // Assigning a value to the property updates the file system immediately
        fileInfo.LastWriteTime = oldDate;
        fileInfo.CreationTime = oldDate; // Also update creation time

        // 3. Confirm the changes
        // You must call Refresh() to update properties in the same instance
        fileInfo.Refresh();

        Console.WriteLine($"[After] Last Write Time: {fileInfo.LastWriteTime}");
        Console.WriteLine($"[After] Creation Time: {fileInfo.CreationTime}");

        // (Reference) Using static methods of the File class
        // File.SetLastWriteTime(filePath, DateTime.Now);
    }
}

Execution Result (Example: If the current date is 2025/10/01)

[Before] Last Write Time: 2025/10/01 10:00:00
[Before] Creation Time: 2025/10/01 10:00:00
--- Modifying timestamps ---
[After] Last Write Time: 2024/10/01 10:00:00
[After] Creation Time: 2024/10/01 10:00:00

Explanation and Technical Points

1. Types of Dates You Can Get and Set

The FileInfo class provides three primary date properties. All of these use the DateTime type and support both reading and writing.

  • LastWriteTime: The time the file content was last modified. This is the most commonly used property.
  • CreationTime: The time the file was created.
  • LastAccessTime: The time the file was last accessed (e.g., for reading).

2. The FileInfo.Refresh() Method

A FileInfo object caches file information when you first access its properties. If the file on the disk changes—whether by your own code or another process—you must call the Refresh() method to update the object with the latest values from the file system. In the sample code, this method ensures the updated timestamps are displayed correctly.

3. Difference Between Static and Instance Methods

If you are performing a single operation on a specific file, the static methods of the File class are convenient because they do not require creating an instance.

  • File.GetLastWriteTime(path)
  • File.SetLastWriteTime(path, dateTime) However, if you are processing many files sequentially or need to handle multiple attributes (like size and dates) together, creating a FileInfo instance is more efficient.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次