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 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(@"October 18, 2006"));
    File.SetLastAccessTime(path, DateTime.Parse(@"October 18, 2006"));
    File.SetLastWriteTime(path, DateTime.Parse(@"October 18, 2006"));
}

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.

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(2006,10,18);
    fileInfo.CreationTime = dt;
    fileInfo.LastAccessTime = dt;
    fileInfo.LastWriteTime = dt;
}

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

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

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

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, and, 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.

Popularity: 2% [?]