To display 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 GetCreationTime, GetLastAccessTime, and GetLastWriteTime. Each has a single parameter, the path to the directory whose timestamp information is to be returned, and returns a DateTime value containing the relevant timestamp. 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());
}

The instance properties of the DirectoryInfo class are CreationTime, LastAccessTime, and LastWriteTime. Each returns a DateTime value containing the respective timestamp from the directory represented by the DirectoryInfo object. The following code illustrates their use:

public void DisplayDirAttr(string path)
{
    DirectoryInfo dirInfo = new DirectoryInfo(path);

    Console.WriteLine(dirInfo.CreationTime.ToString());
    Console.WriteLine(dirInfo.LastAccessTime.ToString());
    Console.WriteLine(dirInfo.LastWriteTime.ToString());
}

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, and SetLastWriteTime. All of them take the path to the directory 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 ModifyDirAttr(string path)
{
    Directory.SetCreationTime(path, DateTime.Parse(@"October 30, 2006"));
    Directory.SetLastAccessTime(path, DateTime.Parse(@"October 30, 2006"));
    Directory.SetLastWriteTime(path, DateTime.Parse(@"October 30, 2006"));
}

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 ModifyDirAttr(string path)
{
    DirectoryInfo dirInfo = new DirectoryInfo(path);

    DateTime dt = new DateTime(2006,10,30);
    dirInfo.CreationTime = dt;
    dirInfo.LastAccessTime = dt;
    dirInfo.LastWriteTime = dt;
}

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

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 ViewModifyDirAttr(string path, FileAttributes fileAttribute)
{
    if (Directory.Exists(path)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(path);

        // Display this directory''s attributes
        Console.WriteLine(dirInfo.Attributes.ToString());

        // 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.Hidden;
        // Display whether this directory is hidden
        Console.WriteLine("Is directory hidden? = " +
       ((dirInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden));
    }
}

The table below lists each of the flags in the FileAttributes enumeration.

Member name Description
Archive The current directory is archived.
Compressed 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 can be set at one time, but see the description for the Normal flag, which must be used alone.

Popularity: 2% [?]