Recipe 12.2. Manipulating File AttributesProblemYou need to display or manipulate a file's attributes or timestamps. SolutionTo display a file's timestamps, you can use either the static methods of the File class or the instance properties of the FileInfo class. The static methods are GetCreationTime, GetLastAccessTime, and GetLastWriteTime. Each has a single parameter, the path and name of the file for which timestamp information is to be returned, and returns a DateTime value containing the relevant timestamp. For example: public static void DisplayFileAttr(string path) { Console.WriteLine(File.GetCreationTime(path)); Console.WriteLine(File.GetLastAccessTime(path)); Console.WriteLine(File.GetLastWriteTime(path)); } The instance properties of the FileInfo class are CreationTime, LastAccessTime, and LastWriteTime. Each returns a DateTime value containing the respective timestamp of the file represented by the FileInfo object. The following code illustrates their use: public static void DisplayFileAttr(string path) { FileInfo fileInfo = new FileInfo(path); Console.WriteLine(fileInfo.CreationTime); Console.WriteLine(fileInfo.LastAccessTime); Console.WriteLine(fileInfo.LastWriteTime); } Using an instance of the FileInfo class is preferable to using the equivalent methods on the static File class (i.e., File.GetCreationTime, File.GetLastAccessTime, and File.GetLastWriteTime) as far as performance is concerned. 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 FileInfo instance object, which already holds this file handle information. To modify a file's timestamps, you can use either the static methods of the File class or the instance properties of the FileInfo class. The static methods are SetCreationTime, SetLastAccessTime, and SetLastWriteTime. All of them take the path and name of the file for which the timestamp is to be modified as the first parameter and a DateTime value containing the new timestamp as the second, and each returns void. For example: public static void ModifyFileAttr(string path) { File.SetCreationTime(path, DateTime.Parse(@"May 10, 2003")); File.SetLastAccessTime(path, DateTime.Parse(@"May 10, 2003")); File.SetLastWriteTime(path, DateTime.Parse(@"May 10, 2003")); } The instance properties are the same as the properties used to display timestamp information: CreationTime, LastAccessTime, or LastWriteTime. To set the timestamp, assign a value of type DateTime to the relevant timestamp property. For example: public static void ModifyFileAttr(string path) { FileInfo fileInfo = new FileInfo(path); DateTime dt = new DateTime(2001,2,8); fileInfo.CreationTime = dt; fileInfo.LastAccessTime = dt; fileInfo.LastWriteTime = dt; } To display or modify a file's attributes, use the instance Attributes property. The property's value is a bit mask consisting of one or more members of the FileAttributes enumeration. For example, the following code: public static void ViewModifyFileAttr(string path) { if (File.Exists(path)) { FileInfo fileInfo = new FileInfo(path); // Display this file's attributes. Console.WriteLine(fileInfo.Attributes); // Display whether this file is hidden. Console.WriteLine("Is file hidden? = {0}", ((fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)); // Modify this file's attributes. fileInfo.Attributes |= FileAttributes.Hidden; } } DiscussionOne of the easier methods of creating a DateTime object is to use the static DateTime.Parse method. This method accepts a string defining a particular date and is converted to a DateTime object. In addition to timestamp information, a file's attributes may also be obtained and modified. This is accomplished through the use of the public instance Attributes property found on a FileInfo object. This property returns or modifies a FileAttributes enumeration. The FileAttributes enumeration is made up of bit flags that can be turned on or off through the use of the bitwise operators &, |, or ^. Table 12-1 lists each of the flags in the FileAttributes enumeration.
In many cases, more than one of these flags can be set at one time, but see the description for the Normal flag, which must be used alone. See AlsoSee the "File Class," "FileInfo Class," and "FileAttributes Enumeration" topics in the MSDN documentation. |