DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 11.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 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));
}

Discussion

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

Table 11-9. Definitions of each bit flag in the FileAttributes enumeration

Flag name

Definition

Archive

The current directory is archived.

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.

    [ Team LiB ] Previous Section Next Section