[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Get and set file times in C#

[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.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.