ASP.NET and Several Methods for Updating a Last Modified Date

By: Joann Lavrich

Page 4 of 4

Set for printing

Previous

Using the File Attributes to Give the Last Modified

Suppose, however, that Dreamweaver could not be used to automatically update the date because another developer did not have access to it. Retrieving the date from the file attributes is another method that can be used to get the date the page was modified. Access to the file’s timestamp can be achieved through either using the instance properties of FileInfo class or the static methods of the File class. The FileInfo class has a series of instance properties that pertain to the timestamping of the file, mainly CreationTime, LastAccessTime and LastWriteTime.   The static methods of the File class are GetCreationTime, GetLastAccessTime and GetLastWriteTime. In this case, the most likely candidate for retrieving the proper timestamp is the LastWriteTime or the last time the file was written to. The CreationTime would be when the file was actually created and the LastAccessTime is the last time someone has opened the file. Remember that when dealing with these times, it is the server time and not the user's local time.

The following example displays the file’s timestamp for the last time it was written to:

public static void DisplayFileAttribute(string path)
{
  Console.WriteLine(File.GetLastWriteTime(path));
}

This method could be used by calling the above mentioned code and setting the path equal to the the path and name of the file that should be modified. Paths may be referenced by the relative path or an absolute path along with the name of the file. As in the following:

DisplayFileAttribute("\somedir\somefile.txt")

Conversely, if the FileInfo instance properties are used:

public static void DisplayFileAttribute(string path)
{
  FileInfo fileInfo = new FileInfo(path);
  Console.WriteLine(fileInfo.LastWriteTime);
}

Setting the File Timestamps in Code

For additional information on file I/O in ASP.NET, please refer to these CMX articles: Working with Files in ASP.NET and Reading From and Writing To Files Using ASP.NET.

One last thing to consider, a little more complex, the timestamp on a particular file is to be set dynamically. As in the previous section, the file timestamps can be modified using either instance properties of FileInfo class or the static methods of the File class. In this case, the FileInfo instance properties are modified and not returned as in the previous section. The static methods of the File class are SetCreationTime, SetLastAccessTime and SetLastWriteTime. A more detailed explanation of these may be found on Microsoft’s site FileInfo Instance Properties and File Methods respectively.

To modify all three of  the file attributes using the FileInfo instance properties, look at the following example:

public static void ModifyFileAttribute(string path)
{
  FileInfo fileInfo = new FileInfo(path);
  DateTime curDate = new DateTime(DateTime.Now)
  fileInfo.CreationTime = curDate;
  fileInfo.LastAccessTime = curDate;
  fileInfo.LastWriteTime = curDate;
}

For the above mentioned methods to work, the internet user would have to have permission on the server to have these properties set. This opens up a host of security issues and is generally not advised. A more in depth explanation of this is discussed in Reading From and Writing To Files Using ASP.NET.

In this example, curDate is set to the current date and time with no formatting of the date. All three of the possible timestamped properties are set to that date and time.

Changing the file attribute while using the File class methods requires passing two parameters: the path and filename; the date value. An example of this is:

public static void ModifyFileAttribute(string path)
{
  File.SetLastWriteTime(path, DateTime.Now);
}

Notice that only the LastWriteTime was modified in that.

Conclusion

Determining when the last time a page’s content was modified and displayed to the end user may be done using a variety of methods. In the end, it really depends on what is going on in the background as to how that information is passed back to the user.

Page 4 of 4 Previous 1 2 3 4


download
Download Support Files


Downloads are disabled during your trial period.


Keywords
created, modified, .net, date, C#, VB, asp.net, timestamp, Dreamweaver, SQL, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, DateTime, FileInfo, file attributes