Title: Get and set file times in C#
The File class provides methods that get and set file times including the creation, modification, and access times. The following code shows how the example uses those methods to get and set a file's times.
// Get the file's times.
private void btnGetTimes_Click(object sender, EventArgs e)
{
txtCreationTime.Text =
File.GetCreationTime(txtFile.Text).ToString();
txtModifiedTime.Text =
File.GetLastWriteTime(txtFile.Text).ToString();
txtAccessTime.Text =
File.GetLastAccessTime(txtFile.Text).ToString();
}
// Set the file's times.
private void btnSetTimes_Click(object sender, EventArgs e)
{
File.SetCreationTime(txtFile.Text,
DateTime.Parse(txtCreationTime.Text));
File.SetLastWriteTime(txtFile.Text,
DateTime.Parse(txtModifiedTime.Text));
File.SetLastAccessTime(txtFile.Text,
DateTime.Parse(txtAccessTime.Text));
}
It's that easy!
Download the example to experiment with it and to see additional details.
|