Get and set file times in C#

[set file times]

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 Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in files and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *