Recipe12.2.Manipulating File Attributes


Recipe 12.2. Manipulating File Attributes

Problem

You need to display or manipulate a file's attributes or timestamps.

Solution

To 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;    } } 

Discussion

One 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.

Table 12-1. FileAttributes enumeration values

Member name

Description

Archive

Represents the file's archive status that marks the file for backup or removal.

Compressed

Indicates that the file is compressed.

Device

This option is reserved for future use.

Directory

Indicates that this is a directory.

Encrypted

Indicates that a file or directory is encrypted. In the case of a file, its contents are encrypted. In the case of a directory, newly created files will be encrypted by default.

Hidden

Indicates a hidden file.

Normal

Indicates that the file has no other attributes; as such, this attribute cannot be used in combination with others.

NotContentIndexed

Indicates that the file is excluded from the content index service.

Offline

Indicates that the state of the file is offline and its contents will be unavailable.

ReadOnly

Indicates that the file is read-only.

ReparsePoint

Indicates a reparse point, a block of data associated with a directory or file.

SparseFile

Indicates a sparse file, which may take up less space on the filesystem than its reported size because zeros in the file are not actually allocated on-disk.

System

Indicates that the file is a system file.

Temporary

Indicates a temporary file. It may reside entirely in memory.


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 Also

See the "File Class," "FileInfo Class," and "FileAttributes Enumeration" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net