How can I get the correct CreationTime for a file?

ZorroRojo

Member
Joined
Oct 9, 2006
Messages
15
Location
Virginia, US
Programming Experience
Beginner
The following code works great the first time I run it.

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
     FileNames(New DirectoryInfo("c:\data"))
 
End Sub
 
Sub FileNames(ByVal dir As DirectoryInfo)
 
     Dim objFile As FileInfo
 
     For Each objFile In dir.GetFiles("*.txt")
          Console.WriteLine(objFile.FullName & " " & objFile.CreationTime & " ")
     Next
 
End Sub

The problem is if I have a file named test.txt created 10/10/2006 12:00PM and then I delete it and create a new one with the same name and then run the above code it still pulls back the create date of 10/10/2006 12:00PM even though the create date I see when looking at the file is 10/10/2006 12:01PM. I see the same thing if I created a file yesterday and delete it and then create one today. It will still use the create date and time from the file I created yesterday.

Does anyone know why this is happening? How can I make it find the correct creation date?

What I am simulating here is receiving a daily import but I never know when the new file will arrive and I want to make sure I don't process the same data more than once. So I have a process that checks each day starting at a certain time and if the date is for today then it imports the data. If the date is not todays then it reschedules itself to run 15 minutes later until a file with todays date is found.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
The FileInfo properties are cached if you reuse a particular instance, which you don't do here, but if you did you would have to call objFile.Refresh() to force new property info. Just telling you this in case you read the documentation about this and misunderstood it.

About deleting a file and then creating a new with same file name I've seen this too, I think it is how the operating system interprets this operation, lastwritetime (modified) changes but not creationtime, that's what I'm seeing in file properties with Windows Explorer.
 

ZorroRojo

Member
Joined
Oct 9, 2006
Messages
15
Location
Virginia, US
Programming Experience
Beginner
Thanks. I started using lastwritetime after I posted here, which does give the correct date and time but I just don't see why it would work the way it does when the file is a different file and when I view it in windows explorer I see the correct date but VB does not see that for the creationtime. And if I have deleted a file then where does it keep finding the date and time from the file I have deleted?

I am going to try and use the Refresh as you put it. I was trying that yesterday, but could not get the syntax correct.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
There is no difference from what Windows Explorer and .Nets FileInfo reports. What Windows says goes.

You only use FileInfo.Refresh if you need to check a property of same FileInfo instance over a period of time, which will never happen in your code.
 
Top Bottom