
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
704
|
Chapter 12: Filesystem I/O
To delete a directory, you must first determine whether it exists. If it does not exist,
the delete operation will fail and throw an exception.
See Also
See the “Directory Class” and “DirectoryInfo Class” topics in the MSDN documenta-
tion.
12.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,orGetLastWriteTime.
For example:
public static void DisplayDirAttr(string path)
{
Console.WriteLine(Directory.GetCreationTime(path));
Console.WriteLine(Directory.GetLastAccessTime(path));
Console.WriteLine(Directory.GetLastWriteTime(path));
}
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. For example:
public static void DisplayDirAttr(string path)
{
DirectoryInfo dirInfo = Directory.CreateDirectory(path);
Console.WriteLine(dirInfo.CreationTime);
Console.WriteLine(dirInfo.LastAccessTime); ...