O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required