Recipe 12.11. Manipulating Directory AttributesProblemYou need to display or manipulate a directory's attributes or timestamps. SolutionTo 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 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); Console.WriteLine(dirInfo.LastWriteTime); } Each property returns a DateTime value containing the timestamp from the directory represented by the DirInfo object. It should be noted that the static counterparts to these properties (i.e., the Directory.GetCreationTime, Directory.GetLastAccessTime, and Directory.GetLastWriteTime methods) perform slower than the instance properties of the DirectoryInfo class. This is because of the extra time it takes for the underlying implementation of the static methods to get information about the file handle each time a static method is called, as opposed to one time for the DirectoryInfo instance object, which already holds this file handle information. 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 ModifyDirAttr(string path) { DateTime dt = new DateTime(2003,5,10); Directory.SetCreationTime(path, dt); Directory.SetLastAccessTime(path, dt); Directory.SetLastWriteTime(path, dt); } Each method has two parameters: the first is the path to the directory with a timestamp that is to be set, and the second is a DateTime value containing the new timestamp. Each method returns void. The instance properties, all of which are of type DateTime, are CreationTime, LastAccessTime, and LastWriteTime. For example: public static void ModifyDirAttr(string path) { DirectoryInfo dirInfo = Directory.CreateDirectory(path); DateTime dt = new DateTime(2001,2,8); dirInfo.CreationTime = dt; dirInfo.LastAccessTime = dt; dirInfo.LastWriteTime = dt; } To display or modify a directory's attributes, use the instance property Attributes: public static void ViewModifyDirAttr(string path, FileAttributes fileAttributes) { DirectoryInfo dirInfo = new DirectoryInfo(@"C:\SomeDir"); // Display this directory's attributes. Console.WriteLine(dirInfo.Attributes); // Display whether this directory is hidden. Console.WriteLine("Is directory hidden? = " + ((dirInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)); // Modify this directory's attributes. dirInfo.Attributes |= fileAttributes; // Display whether this directory is hidden. Console.WriteLine("Is directory hidden? = " + ((dirInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)); } The output of this code is shown here: Directory Is directory hidden? = False Is directory hidden? = True DiscussionThere are three distinct timestamps associated with any directory. These timestamps are its creation time, its last access time, and its last write time. In addition to timestamp information, a directory's attributes may also be obtained and modified. This is accomplished through the use of the public instance Attributes property found on a DirectoryInfo object. This property returns the FileAttributes enumeration value (see Table 12-9). The FileAttributes enumeration is made up of bit flags that can be turned on or off through the use of the bitwise operators &, |, or ^.
In many cases, more than one of these flags may be set at one time. The Normal flag is the exception; when this flag is set, no other flag may be set. See AlsoSee the "Directory Class," "DirectoryInfo Class," and "FileAttributes Enumeration" topics in the MSDN documentation. |