
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
678
|
Chapter 12: Filesystem I/O
File.SetLastAccessTime(path, DateTime.Parse(@"May 10, 2003"));
File.SetLastWriteTime(path, DateTime.Parse(@"May 10, 2003"));
}
The instance properties are the same as the properties used to display timestamp
information:
CreationTime, LastAccessTime,orLastWriteTime. To set the timestamp,
assign a value of type
DateTime to the relevant timestamp property. For example:
public static void ModifyFileAttr(string path)
{
FileInfo fileInfo = new FileInfo(path);
DateTime dt = new DateTime(2001,2,8);
fileInfo.CreationTime = dt;
fileInfo.LastAccessTime = dt;
fileInfo.LastWriteTime = dt;
}
To display or modify a file’s attributes, use the instance Attributes property. The
property’s value is a bit mask consisting of one or more members of the
FileAttributes enumeration. For example, the following code:
public static void ViewModifyFileAttr(string path)
{
if (File.Exists(path))
{
FileInfo fileInfo = new FileInfo(path);
// Display this file's attributes.
Console.WriteLine(fileInfo.Attributes);
// Display whether this file is hidden.
Console.WriteLine("Is file hidden? = {0}",
((fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden));
// Modify this file's attributes.
fileInfo.Attributes |= FileAttributes.Hidden;
}
}
Discussion
One of the easier methods ...