[ Team LiB ] |
Recipe 11.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 whose timestamp information is to be returned, and returns a DateTime value containing the relevant timestamp. For example: public void DisplayFileAttr(string path) { Console.WriteLine(File.GetCreationTime(path).ToString( )); Console.WriteLine(File.GetLastAccessTime(path).ToString( )); Console.WriteLine(File.GetLastWriteTime(path).ToString( )); } 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 void DisplayFileAttr(string path) { FileInfo fileInfo = new FileInfo(path); Console.WriteLine(fileInfo.CreationTime.ToString( )); Console.WriteLine(fileInfo.LastAccessTime.ToString( )); Console.WriteLine(fileInfo.LastWriteTime.ToString( )); } 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 whose 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 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 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 bitmask consisting of one or more members of the FileAttributes enumeration. For example, the following code: public void ViewModifyFileAttr(string path, FileAttributes fileAttribute) { if(File.Exists(path) { FileInfo fileInfo = new FileInfo(path); // Display this file's attributes Console.WriteLine(fileInfo.Attributes.ToString( )); // Display whether this file is hidden Console.WriteLine("Is file hidden? = " + ((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 11-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. |
[ Team LiB ] |