This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 1.1+

File and Folder Timestamps

Microsoft Windows maintains three timestamps for all files and folders. These are the creation time and the times that the item was last accessed and updated. These timestamps can be accessed and updated using the .NET framework.

File System Metadata

All files and folders controlled by Microsoft Windows operating systems include a number of timestamps. These allow you to determine when a file or folder was created, when it was last updated and the time that it was most recently accessed. This information can be useful when you are writing software that interacts with the file system. For example, you may create a utility that performs incremental backups, only storing files that have been modified since the previous backup was taken.

There are several ways to obtain and modify the three timestamps. We'll look at two possibilities. The first uses instances of the FileInfo and DirectoryInfo classes, which provide access to file and folder metadata, including the timestamps.

Obtaining FileInfo and DirectoryInfo Instances

When you want to obtain information relating to a file you can use the FileInfo class. The simplest way to create an instance of this class is to use a constructor that receives the full path and name of the file to be queried, or a path relative to the current folder, as shown below:

FileSystemInfo info = new FileInfo(@"c:\demo\test.txt");

Note that in the above sample code we've populated a variable of the FileSystemInfo type. This is an abstract class that is the base class for the FileInfo and DirectoryInfo classes. It includes the properties that we need for interrogating files and folders. Of course, you could assign the new object to a FileInfo variable instead.

When working with folders, the DirectoryInfo constructor is used in a similar manner, this time passing the full path or a relative path to the desired folder.

FileSystemInfo info = new DirectoryInfo(@"c:\demo");

File and Folder Timestamps

Once you have an instance of the FileInfo or DirectoryInfo class, you can use six properties, which are defined in the FileSystemInfo base class, to retrieve the timestamps. The properties are as follows:

  • CreationTime. The date and time that the file or folder was created, returned using the local time zone details.
  • CreationTimeUtc. The time that the file or folder was created, returned using co-ordinated universal time (UTC).
  • LastAccessTime. The local time at which the file or folder was last accessed.
  • LastAccessTimeUtc. The UTC time when the file or folder was last accessed.
  • LastWriteTime. The date and time at which the file or folder was last updated, using the user's local time zone settings.
  • LastWriteTimeUtc. The UTC time that the file or folder was last updated.

The following code outputs all of the timestamps for the file, "c:\demo\test.txt". NB: The constructors for the two types do not check that the file or folder exists. If the item is not present, the timestamps will return 1 January 1601.

FileSystemInfo info = new FileInfo(@"c:\demo\test.txt");

Console.WriteLine(new string('-', info.FullName.Length));

Console.WriteLine("Created at {0} - {1} UTC"
    , info.CreationTime, info.CreationTimeUtc);

Console.WriteLine("Last accessed at {0} - {1} UTC"
    , info.LastAccessTime, info.LastAccessTimeUtc);

Console.WriteLine("Last written to at {0} - {1} UTC"
    , info.LastWriteTime, info.LastWriteTimeUtc);

The six properties are writeable. When they are set, the operating system updates the file on disk, allowing you to change the creation, update and access times for the files. This can be useful when creating "Touch" utilities.

FileSystemInfo info = new FileInfo(@"c:\demo\test.txt");
info.CreationTime = new DateTime(2012, 9, 24);
info.LastAccessTime = new DateTime(2012, 9, 24, 12, 34, 56);
info.LastWriteTime = new DateTime(2012, 9, 24, 1, 23, 45);

Using the File and Directory Classes

If you are reading more than one timestamp, or if you need other file information, the DirectoryInfo and FileInfo classes are ideal. If you only wish to read or change a single timestamp, you should also consider using static methods from the File and Directory classes. Both classes include six methods that retrieve the three timestamps as local or UTC times. The methods are named, GetCreationTime, GetCreationTimeUtc, GetLastAccessTime, GetLastAccessTimeUtc, GetLastWriteTime and GetLastWriteTimeUtc. Each accepts the name of a file or folder using a string parameter.

Console.WriteLine("Created at {0}", File.GetCreationTime(@"c:\demo\test.txt"));
Console.WriteLine("Created at {0} UTC", File.GetCreationTimeUtc(@"c:\demo\test.txt"));

Console.WriteLine("Accessed at {0}", File.GetLastAccessTime(@"c:\demo\test.txt"));
Console.WriteLine("Accessed at {0} UTC", File.GetLastAccessTimeUtc(@"c:\demo\test.txt"));

Console.WriteLine("Updated at {0}", File.GetLastWriteTime(@"c:\demo\test.txt"));
Console.WriteLine("Updated at {0} UTC", File.GetLastWriteTimeUtc(@"c:\demo\test.txt"));

There are also static methods to allow you to change the timestamps. These are SetCreationTime, SetCreationTimeUtc, SetLastAccessTime, SetLastAccessTimeUtc, SetLastWriteTime and SetLastWriteTimeUtc. The local time versions are used in the following sample code:

File.SetCreationTime(@"c:\demo\test.txt", new DateTime(2012, 9, 24));
File.SetLastAccessTime(@"c:\demo\test.txt", new DateTime(2012, 9, 24, 12, 34, 56));
File.SetLastWriteTime(@"c:\demo\test.txt", new DateTime(2012, 9, 24, 1, 23, 45));
24 September 2012