11.11. 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 void DisplayDirAttr(string path)
{
Console.WriteLine(Directory.GetCreationTime(path).ToString( ));
Console.WriteLine(Directory.GetLastAccessTime(path).ToString( ));
Console.WriteLine(Directory.GetLastWriteTime(path).ToString( ));
}In each case, path is the path to the
directory whose timestamp you wish to retrieve, and the method
returns a DateTime value containing the relevant
timestamp. The
instance
properties are
CreationTime, LastAccessTime,
or LastWriteTime. For example:
public void DisplayDirAttr(string path)
{
DirectoryInfo dirInfo = Directory.CreateDirectory(path);
Console.WriteLine(dirInfo.CreationTime.ToString( ));
Console.WriteLine(dirInfo.LastAccessTime.ToString( ));
Console.WriteLine(dirInfo.LastWriteTime.ToString( ));
}Each property returns a DateTime value containing
the timestamp from the directory represented by the
DirInfo object.
To modify a directory’s timestamps, you can use
either set of static methods of the Directory
class or a set of instance properties of the
DirectoryInfo class.
The
static methods are SetCreationTime,
SetLastAccessTime, or
SetLastWriteTime ...
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