Recipe12.11.Manipulating Directory Attributes


Recipe 12.11. Manipulating Directory Attributes

Problem

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

Solution

To 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 

Discussion

There 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 ^.

Table 12-8. Definitions of each bit flag in the FileAttributes enumeration

Flag name

Definition

Archive

Typically, backup applications will use this to indicate the archive status of the file.

Compress

The current directory uses compression.

Directory

The current item is a directory.

Encrypted

The current directory is encrypted.

Hidden

The current directory is hidden.

Normal

The current directory has no other attributes set. When this attribute is set, no others can be set.

NotContentIndexed

The current directory is not being indexed by the indexing service.

Offline

The current directory is offline, and its contents are not accessible unless it is online.

ReadOnly

The current directory is read-only.

ReparsePoint

The current directory contains a reparse point.

SparseFile

The current directory contains large files consisting mostly of zeros.

System

The current directory is used by the system.

Temporary

The current directory is classified as a temporary directory.


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 Also

See the "Directory Class," "DirectoryInfo 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