12.4. Manipulating Directory Attributes
Problem
You need to display or manipulate a directory's attributes or timestamps.
Solution
To display a directory's timestamps, you can use either the set of static methods from the Directory object or the set of instance properties from the DirectoryInfo object. The static methods are GetCreationTime, GetLastAccessTime,or GetLastWriteTime. For example:
public static void DisplayDirectoryTimestamps(string path)
{
Console.WriteLine(Directory.Get CreationTime(path).ToString());
Console.WriteLine(Directory.Get LastAccessTime(path).ToString());
Console.WriteLine(Directory.Get LastWriteTime(path).ToString());
}In each case, path is the path to the directory with a timestamp you wish to retrieve, and the method returns a DateTime value containing the relevant timestamp. The instance properties are CreationTime, LastAccessTime,or LastWriteTime. The DisplayTimestamps extension method for DirectoryInfo allows accessing all of these at once:
public static void DisplayTimestamps(this DirectoryInfo dirInfo)
{
Console.WriteLine(dirInfo.CreationTime.ToString());
Console.WriteLine(dirInfo.LastAccessTime.ToString());
Console.WriteLine(dirInfo.LastWriteTime.ToString());
}To modify a directory's timestamps, you can use either the static methods of the Directory class or the instance properties of the DirectoryInfo class. The static methods are SetCreationTime, SetLastAccessTime, or SetLastWriteTime. For example:
public static void ModifyDirectoryTimestamps(string ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access