While everyone knows that you can manipulate text files and their contents via an ASP.NET Web page,
did you know that you can actually modify a file's timestamps just as easily? Why would you
want to do that? Read on to find out.
While it's not something that everyone will have a need for, in certain situations it can be
quite helpful to be able to modify a file's creation or last modified timestamp. While
I'm sure you can probably come up with other reasons, I use it mainly to keep track of
which files are modified after I post them to a client's site. If I set the last
modified time so that it's the same for all the files in a project, then it's an
extremely simple matter to tell at a glance which files have been changed after
the client calls you saying their site is broken and they "haven't changed anything".
I'm always happy to fix the errors that have been introduced... this just instantly tells
me where to look to find them.
The sample code below illustrates the basic process. Here I simply change one file's
properties, but changing a large number of files is simply a matter of looping through
the files you want to modify and changing them on each file.
Dim myDateTime As Date = New Date(2000, 1, 1, 12, 34, 56)
Dim myFileInfo As FileInfo
' Get a handle on the file we're manipulating
myFileInfo = New FileInfo(Server.MapPath("test.htm"))
' Change timestamps to new values
myFileInfo.CreationTime = myDateTime
myFileInfo.LastWriteTime = myDateTime
That's all there is to it. ASP.NET makes changing the values just as easy as reading them.
Just make sure you log in as a user that has the appropriate (NTFS) permissions needed to modify
the files you're working with.