Fileinfo.CreateText generating wrong CreationTime on new file

G

Guest

This seems a trival question, but I cannot get it to work properly...
Essentially my question is, how can I create a text file, and guarantee it is
given the current date/time as the Creation Time?

My code is creating a file correctly, but the CreationTime is (sometimes)
set to the CreationTime of an old file that has previously been removed.

The line of code creating the file is
_LogFileWriter = newFile.CreateText()

In detail: the utility is to return a StreamWriter to a LogFile - if the
LogFile is more than 1 week old, it is copied to another name, and a fresh
log file is created. Should be simple enough...

The sequence is to check the date on LogFileName, if it is older than last
Sunday's date, then move the file to LogFileName.0, and Create LogFileName.
The problem is that LogFileName is given the CreationTime of the previous
LogFileName, not the current date and time.

Also, after the Move operation, the CreationTime for LogFileName.0 is set to
the CreationTime of the previous LogFileName.0, instead of retaining the
CreationTime of the source file, LogFileName (however, this is not actually
causing a problem, just seems strange).

The full code is shown below. Note that the line
oldFile.CreationTime() = Now.AddDays(-9)
is only for debugging, to simulate finding an old file. It is commented out
in production.
There is also code to "rollback" the old log files to a specified "depth" -
this all seems to be working OK, and can be ignored.

Platform is Windows XP SP2.

I must be missing something simple, if anyone can point me in the right
direction it would be greatly appreciated.

Regards,
Tim

Code:
Public Sub New(ByVal LogFileName As String, ByVal Append As Boolean, ByVal
RollbackDepth As Integer)
' Usage: RollbackDepth - is the number of file levels to rollback.
' RollbackDepth = 0 - no rollback
' If RollbackDepth > 0 then Append = true
' For ease of reading, break into 2 branches:
' - no file rollback (and LogFileName does not exist)
' - file rollback
Dim FileIndex As Integer
Dim thisFileName As String
Dim referenceDate As DateTime
Dim dayIndex As Integer 'represents day of week, used to calculate
date of previous sunday
Dim oldFile, newFile As FileInfo 'Was using the File object, but
getting weird behaviour on dates, so try
' using FileInfo Instance methods instead.
' Continue to use the Static File methods where files are not being
created, and for rollback move operations.

'*** Branch: no file rollback (and LogFileName does not exist)
If (File.Exists(LogFileName)) Then
If RollbackDepth = 0 Then
If Append Then
_LogFileWriter = File.AppendText(LogFileName)
Else
File.Delete(LogFileName)
_LogFileWriter = File.CreateText(LogFileName)
End If
Exit Sub 'Exit Branch: no file rollback (and LogFileName
does not exist)
End If 'RollbackDepth > 0
Else 'LogFileName does not exist
_LogFileWriter = File.CreateText(LogFileName)
Exit Sub 'Exit Branch: no file rollback (and LogFileName does
not exist)
End If '(File.Exists(LogFileName))

'*** Branch: file rollback, RollbackDepth > 0
' Look at LogFileName creation date
dayIndex = CType(Now.DayOfWeek, Integer) '0 Sunday, 1 Monday, 2
Tuesday, 3 Wednesday ...
referenceDate = Now.AddDays(-dayIndex) 'Get last Sunday's date
'following 2 lines are for debug purposes, simulates encountering an
old log file to be rolled back
oldFile = New FileInfo(LogFileName)
oldFile.CreationTime() = Now.AddDays(-9) 'Only used to simulate an
old file for testing purposes, comment out in production
If oldFile.CreationTime < referenceDate Then
thisFileName = LogFileName & "." & RollbackDepth.ToString
If File.Exists(thisFileName) Then File.Delete(thisFileName)
'always have to delete last file
FileIndex = RollbackDepth - 1
While FileIndex >= 0
thisFileName = LogFileName & "." & FileIndex.ToString
If (File.Exists(thisFileName)) Then
File.Move(thisFileName, LogFileName & "." & (FileIndex +
1).ToString)
End If
FileIndex -= 1
End While
'Handle the case for LogFileName
File.Move(LogFileName, LogFileName & ".0")
newFile = New FileInfo(LogFileName)
_LogFileWriter = newFile.CreateText() '*** file gets created
with old creation date??? ***
Else
_LogFileWriter = File.AppendText(LogFileName)
End If 'oldFile.GetCreationTime(LogFileName) < referenceDate
End Sub
 
G

Guest

Can anyone help with this question? Any comments or advice would be
appreciated.
Thanks,
Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top