[ Team LiB ] |
Recipe 11.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 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. For example: public 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 whose timestamp 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 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 void ViewModifyDirAttr(string path, FileAttributes fileAttributes) { DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Windows\System32"); // 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)); } DiscussionThere are three distinct timestamps associated with any particular directory. These timestamps are creation time, last access time, and 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 either returns or modifies a FileAttributes enumeration (see Table 11-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. |
[ Team LiB ] |