Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Windows never forgets a file: Using the CreationTime attribute of Windows Files
 »  Home  »  Windows Development  »  Windows never forgets a file: Using the CreationTime attribute of Windows Files
Windows never forgets a file: Using the CreationTime attribute of Windows Files
by Scott Rutherford | Published  02/22/2005 | .NET Newbie Windows Development | Rating:
Scott Rutherford
Scott Rutherford is a consultant and .NET developer who has developed Enterprise business applications in many fields including Restaurant, Market Research, Automotive, and Analytical Chemistry. He is an Microsoft Certified Professional with the .NET Platform. 

View all articles by Scott Rutherford...
Problem and Solution

The Problem

I recently found several bugs in my own programs relating to the way I had been interpreting the creation time of files. All of these bugs resulted from some variation of the following sequence of events:

  • Create file C:\temp\MyFile
  • Delete file C:\temp\MyFile
  • Create file C:\temp\MyFile

My [false] assumption was that the creation time of MyFile is always equal to the last time it was created. This was, after all, the “creation time”. One program of mine, for example, writes and deletes a particular log file once per day—why then was I finding instances where this file had a creation date several months old? In short, the answer is that I was not setting it.

Some Background

The .NET Class Library description of the CreationTime property [of the FileInfo class] is somewhat cryptic (Fig. 1). Does this mean the property gets the time when the object is instantiated—e.g. by calling New FileInfo(), or the time when the underlying file is created—e.g. by calling FileInfo.Create()? It turns out that neither is true. However, it is true that this property sets the time when the underlying file is created.

System.IO.FileSystemInfo.CreationTime Property
Gets or sets the creation time of the current FileSystemInfo object.
Remarks
When first called, FileSystemInfo calls Refresh and returns the cached information on APIs to get attributes and so on. On subsequent calls, you must call Refresh to get the latest copy of the information.
Figure 1: Excerpts from MSDN online reference, .NET Framework Class Library

The Test

In order to test this suspicious property I wanted to remove the .NET Framework from the equation. The logic was that if I could show how the OS should act apart from the .NET Framework, then I could illustrate the bug. I ran a series of tests employing Windows Explorer: I created and deleted files from the same directory, using the file property dialog to examine the timestamps placed on these activities by the OS. Following the Windows Explorer test, I created a simple console app (Fig. 2) that would create and delete the same file 16 times.

The Results

I was once again surprised by the results (Tables 1-2). Though the patterns varied between replications of my test, Windows Explorer demonstrated the same behaviors I had seen in my own apps. It left file creation attributes unchanged after many manipulations. Even a full system reboot after step 2 (i.e. Delete File, Create File), for example, did not change the File Creation Time established in step 1 (Original Create).

The Microsoft product references for both XP and Win2K3 include some details about the way NTFS works. Although File Creation Time is not mentioned in either article, the following statement is made in both regarding the Last Update Time attribute:

NTFS does not update a file’s directory entry when all in-memory references to that file are gone

Although my testing showed that the file attributes seem to catch up eventually, after certain types of manipulations, my experience in the real-world shows that File Creation Time can go unchecked for literally months. Hence, it would seem the only solution to this problem is to  set the Creation Time attribute explicitly whenever a file is created.

Appendix A: Test Results

Table 1: File Create / Delete Test Results (WIN2KSP4)
Action and Time Performed Win Explorer File Properties

Actual Created Modified Accessed
Create File 3:39:56 3:39:56 3:39:56 3:39:56
Delete File, Create File 3:40:39 3:39:56 3:39:56 3:39:56
Rename File 3:41:30 3:39:56 3:40:39 3:40:39
Delete Renamed File, Create File 3:41:54 3:41:54 3:41:54 3:41:54
Delete File, Create File 3:43:00 3:41:54 3:41:54 3:41:54
Rename File 3:43:25 3:39:56 3:40:39 3:40:39
Change Contents and Refresh 3:43:55 3:39:56 3:40:39 3:40:39
Rename to Original Name 3:44:25 3:41:54 3:41:54 3:41:54
See VB.NET code below for Programmatic Delete/Create
16 X Programmatic Delete/Create 3:46:21 3:41:54 N/A N/A
16 X Programmatic Delete/Create 3:46:26 3:46:21 N/A N/A
16 X Programmatic Delete/Create 3:46:31 3:46:21 N/A N/A
16 X Programmatic Delete/Create 3:46:36 3:46:21 N/A N/A

Table 2: File Create / Delete Test Results (WINXPSP2)
Action and Time Performed Win Explorer File Properties

Actual Created Modified Accessed
Create File 2:15:30 2:15:30 2:15:30 2:15:30
Delete File, Create File 2:16:26 2:15:30 2:16:26 2:16:26
Rename File 2:17:44 2:15:30 2:16:26 2:16:33
Delete Renamed File, Create File 2:18:33 2:18:33 2:18:33 2:18:33
Delete File, Create File 2:20:50 2:18:33 2:20:50 2:20:50
Rename File 2:21:57 2:18:33 2:20:50 2:20:56
Change Contents and Refresh 2:22:55 2:18:33 2:22:55 2:22:55
Rename to Original Name 2:23:35 2:18:33 2:22:55 2:23:01

Appendix B: VB.NET code

    Sub Main(ByVal Args() As String)
        Dim fi As System.IO.FileInfo
        Dim fs As System.IO.FileStream
        fi = New System.IO.FileInfo(Args(0))
        If Not fi.Exists Then
            fs = fi.Create
            fs.Close()
            'fi.CreationTime = Now ' THIS IS THE LINE YOU NEED
        End If
        fi.Refresh()
        Console.WriteLine(fi.CreationTime.ToString)
        Dim i As Integer = 0
        For i = 0 To 15
            fi.Delete()
            fi.Refresh()
            fs = fi.Create
            fs.Close()
            'fi.CreationTime = Now ' THIS IS THE LINE YOU NEED
            fi.Refresh()
            Console.WriteLine(fi.CreationTime.ToString)
        Next

        fi.Delete()
        fi.Refresh()
    End Sub
Figure 2: VB.NET console app to create/delete file 16 times

 

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.91836734693878 out of 5
 49 people have rated this page
Article Score16798
Related Articles
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 02/23/2005)
Rating
Very detailed. I knew the windows file times were quirky, but I did not realize just how quirky. Windows XP does behave more like I would expect it to, and it is helpful to know that the creation time is tied to the original document.
 
Comment #2  (Posted by (the author) on 03/08/2005)
Rating
This behaviour is called "file tunneling".
It is apparently a "feature" of FAT and NTFS.
It can be disabled with a Reg setting: http://support.microsoft.com/kb/q172190
If you cannot presume this setting is off on the machine where your program runs:
1. always set the CreationTime explicitly as above
2. or, remove the entire directory when you remove a file, and the recreate the entire DIR, as this empties the file tunneling cache
 
Comment #3  (Posted by an unknown user on 03/20/2005)
Rating
Fine: All Infos given:
a) fact AND Solution in 1st paragraph!
b) done recherches
c) shows data and code
d) restricted to exact the required length not more pages :-)

 
Comment #4  (Posted by Jan Staecker on 03/20/2005)
Rating
Fine: All Infos given:
a) fact AND Solution in 1st paragraph!
b) done recherches
c) shows data and code
d) restricted to exact the required length not more pages :-)

 
Comment #5  (Posted by an unknown user on 04/06/2005)
Rating
This is how all articles should be - Precise and Up to the point and No crap!!
 
Comment #6  (Posted by an unknown user on 08/11/2005)
Rating
Scott's explaination of a problem that many have probably encounter but few research was excellent. By providing not only written explaination but also graphical examples (on 2 separate OS') and the code used to test his theory.

Great Job!
 
Comment #7  (Posted by an unknown user on 10/21/2005)
Rating
It is a shame that u should relay on us developers to correctly explain what u meant to deliver to us in first place !!!
 
Comment #8  (Posted by Benny Carmeli on 10/21/2005)
Rating
PLS see comment #5 above ... ;-)
 
Comment #9  (Posted by an unknown user on 01/30/2006)
Rating
The average rating is: No-one else has rated this article yet
 
Comment #10  (Posted by an unknown user on 03/10/2006)
Rating
Just what I needed to know. Very helpful - thanks!
 
Comment #11  (Posted by an unknown user on 03/10/2006)
Rating
What #5 said - again!
 
Comment #12  (Posted by an unknown user on 03/13/2006)
Rating
very useful
 
Comment #13  (Posted by an unknown user on 06/27/2006)
Rating
Very Good
 
Comment #14  (Posted by an unknown user on 12/11/2006)
Rating
Very nice bit of investigation. Scott's article provides the explanation of an issue I've been having. Even though I figured it out the hard way, it's nice to have confirmation.
 
Comment #15  (Posted by an unknown user on 03/13/2007)
Rating
If a file has size of 0, its access time will never be updated in the XP.
 
Comment #16  (Posted by an unknown user on 08/24/2007)
Rating
Interesting article which I stumbled on while googling for something else.

Concise but thorough piece of research. 5* rating well deserved.
 
Comment #17  (Posted by an unknown user on 08/24/2007)
Rating
Interesting article which I stumbled on while googling for something else.

Concise but thorough piece of research. 5* rating well deserved.
 
Comment #18  (Posted by an unknown user on 09/07/2007)
Rating
Awesome. Thought I was going crazy.
 
Comment #19  (Posted by an unknown user on 09/07/2007)
Rating
Awesome. Thought I was going crazy.
 
Comment #20  (Posted by an unknown user on 01/07/2008)
Rating
Superb. Your analysis is perfect. Great...
 
Comment #21  (Posted by an unknown user on 01/07/2008)
Rating
Superb. Your analysis is perfect. Great...
 
Comment #22  (Posted by an unknown user on 07/09/2008)
Rating
Very good article! I thought I was going crazy!
 
Comment #23  (Posted by an unknown user on 04/20/2009)
Rating
Great article dear...

I mean u have gone to the skin of issue and probably save many frustating hours for thousands of developers across the globe. Thanks alot again .. i was having the same issue .. Wonder why it hasnt been highlighted enough....??
 
Comment #24  (Posted by Naveen Kokcha on 04/20/2009)
Rating
Wonderdful article dear...

U got to skin of this issue and elaborated it wonderfully . let me tell u u saved many frustating hours of Debugging for developers across the globe.. Thanks alot for that.. I wonder why this issue hasnt been highlighted enough.....??? Naveen (MCTS.Net)
 
Sponsored Links